On Sun, Jul 26, 2009 at 3:58 AM, Aaron Burghardt<abu...@mac.com> wrote:
> Why use Core Foundation?  How about (written in Mail):

Thanks for your reply. I ran a test, and it seems CF is slightly faster:

2009-07-26 18:11:52.545 tst[31180:813] CF: Read 4611 tracks in 0.505728 seconds
2009-07-26 18:11:53.585 tst[31180:813] NS: Read 4611 tracks in 0.545601 seconds
2009-07-26 18:11:54.547 tst[31180:813] CF: Read 4611 tracks in 0.507425 seconds
2009-07-26 18:11:55.580 tst[31180:813] NS: Read 4611 tracks in 0.539849 seconds
2009-07-26 18:11:56.543 tst[31180:813] CF: Read 4611 tracks in 0.503465 seconds
2009-07-26 18:11:57.568 tst[31180:813] NS: Read 4611 tracks in 0.528393 seconds
2009-07-26 18:11:58.542 tst[31180:813] CF: Read 4611 tracks in 0.501907 seconds
2009-07-26 18:11:59.585 tst[31180:813] NS: Read 4611 tracks in 0.545226 seconds
2009-07-26 18:12:00.546 tst[31180:813] CF: Read 4611 tracks in 0.506640 seconds
2009-07-26 18:12:01.587 tst[31180:813] NS: Read 4611 tracks in 0.547467 seconds
2009-07-26 18:12:02.547 tst[31180:813] CF: Read 4611 tracks in 0.507250 seconds
2009-07-26 18:12:03.604 tst[31180:813] NS: Read 4611 tracks in 0.564047 seconds

Both methods seem robust, no crashes so far. So, I'm happy for now :)

>
> - (NSDictionary *)dictionaryFromPropertyListFile:(NSString *)path
> error:(NSError **)outError
> {
>        NSData *data = [NSData dataWithContentsOfFile:path options:NULL
> error:&outError];
>        if (!data) return nil;
>
>        NSString *errorString;
>        NSDictionary *plist = [NSPropertyListSerialization
> propertyListFromData:data
>
>  mutabilityOption:NSPropertyListImmutable
>
>    format:NULL
>
>  errorDescription:&errorString];
>        if (!plist) {
>                NSDictionary *info = [NSDictionary
> dictionaryWithObject:errorString forKey:NSLocalizedDescriptionKey];
>                *outError = [NSError errorWithDomain:@"MyAppDomain" code:-1
> userInfo:info];
>                return nil;
>        }
>        return plist;
> }
>
> If there is a read error, the NSData method should be robust enough to catch
> it and just return a nil object. If the data is truncated, modified, or
> corrupted during the read, NSPropertyListSerialization should detect it and
> return nil.  If you are really concerned, wrap it all up in a @try/@catch.
>  Call it, if it fails, wait a couple seconds and try again.  If it continues
> to fail for 2 to 4 attempts, give up and report an error to the user.
>
> Also, it should be faster to just read the content of the file into an
> NSData than to open the file, parse the contents into a plist, then close
> the file (assuming that is what your other approaches are doing.)
>
> Also also, I suggest a quick-and-dirty test.  Write a simple command line
> tool that calls this method repeatedly as fast as possible and logs all
> errors.  Start it up and modify your iTunes library to see if you can induce
> any failure points.
>
> Or use ScriptingBridge.
>
> Regards,
>
> Aaron
>
>
> On Jul 25, 2009, at 4:53 PM, slasktrattena...@gmail.com wrote:
>
>> On Sat, Jul 25, 2009 at 10:21 PM, Kyle Sluder<kyle.slu...@gmail.com>
>> wrote:
>>>
>>> Also not a safe option; other Apple apps can access the XML file,
>>> includeing
>>> CoreServices (for the media picker in the Open panel). Unfortunately we
>>> don't know how they do it and therefore can't be guaranteed that they
>>> won't
>>> also break if you have a lock on the database file.
>>
>> Thanks for the heads up. I'm afraid the end users would be upset if
>> the app required iTunes to be running all of a sudden, though. So I'll
>> try with the Core Foundation methods for now (without locking the
>> file). Hopefully it won't result in a crash if the file is modified
>> while read, just a NULL pointer or invalid data (which is fine
>> considering how often the data is read, I'll just rely on the last
>> valid object).
>>
>> CFPropertyListRef CreateMyPropertyListFromFile( CFURLRef fileURL ) {
>>  CFPropertyListRef propertyList;
>>  CFStringRef       errorString;
>>  CFDataRef         resourceData;
>>  Boolean           status;
>>  SInt32            errorCode;
>>
>>  // Read the XML file.
>>  status = CFURLCreateDataAndPropertiesFromResource(
>>              kCFAllocatorDefault,
>>              fileURL,
>>              &resourceData,            // place to put file data
>>              NULL,
>>              NULL,
>>              &errorCode);
>>
>>  // Reconstitute the dictionary using the XML data.
>>  propertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
>>              resourceData,
>>              kCFPropertyListImmutable,
>>              &errorString);
>>
>>  if (resourceData) {
>>       CFRelease( resourceData );
>>  } else {
>>       CFRelease( errorString );
>>   }
>>
>>  return propertyList;
>> }
>> _______________________________________________
>>
>> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> http://lists.apple.com/mailman/options/cocoa-dev/aaron.burghardt%2Bcocoa-dev%40gmail.com
>>
>> This email sent to aaron.burghardt+cocoa-...@gmail.com
>
>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to