Is it good practice to store NSString data in CFTreeRef using CFTreeContext info object

| | August 11, 2015

I’m trying to create a tree from DB to create custom table. What i’m doing is fetching data from DB and creating tree structure. To create child nodes i’m storing values in Cftree using info object. Its working fine for me. Anyone could you please let me know will this approach have any performance issues. Because i’m storing a NSString in a info object of each node of a tree. If its having performance issues what’s the best way of doing this.
I’m using below code block to create Tree.

static CFTreeRef CreateMyTree(CFTypeRef rootObject) {
CFTreeContext ctx;
ctx.version = 0;
ctx.info = rootObject;
ctx.retain = CFRetain;
ctx.release = CFRelease;
ctx.copyDescription = CFCopyDescription;  
return CFTreeCreate(NULL, &ctx);
}

One Response to “Is it good practice to store NSString data in CFTreeRef using CFTreeContext info object”

  1. There’s no problem with using NSStrings as the data of a CFTree. It’s treated much the same as any other data. NSString is toll-free bridged to CFString, so using CFRetain, CFRelease, and CFCopyDescription for the callbacks is appropriate.

    If you’re using ARC, you will need to use a (__bridge CFStringRef) cast to get the pointer.

    Note that the tree and your callbacks don’t make a copy of the string. So, if you put a reference to an NSMutableString into the tree and subsequently mutate it, that will change the data of the tree. That may or may not be what you want. Considering making a copy before adding it to the tree.

    If you are having performance issues, you’ll need to explain the nature of those performance issues and why you suspect the use of NSString in a CFTree as the cause. If you merely suspect performance issues, then don’t. Until you’ve measured a performance issue, you’re wasting your time.

Leave a Reply