• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDictionary crash
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDictionary crash


  • Subject: Re: NSDictionary crash
  • From: "email@hidden" <email@hidden>
  • Date: Sun, 26 Jul 2009 18:17:07 +0200

On Sun, Jul 26, 2009 at 3:58 AM, Aaron Burghardt<email@hidden> 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, email@hidden wrote:
>
>> On Sat, Jul 25, 2009 at 10:21 PM, Kyle Sluder<email@hidden>
>> 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 (email@hidden)
>>
>> 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:
>>
>>
>> This email sent to email@hidden
>
>
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden

References: 
 >Re: NSDictionary crash (From: Kyle Sluder <email@hidden>)
 >Re: NSDictionary crash (From: "email@hidden" <email@hidden>)
 >Re: NSDictionary crash (From: Kyle Sluder <email@hidden>)
 >Re: NSDictionary crash (From: "email@hidden" <email@hidden>)
 >Re: NSDictionary crash (From: Aaron Burghardt <email@hidden>)

  • Prev by Date: Re: How do I extract the int value from an xml node/element?
  • Next by Date: Re: NSScrollView Not Updating
  • Previous by thread: Re: NSDictionary crash
  • Next by thread: Re: NSDictionary crash
  • Index(es):
    • Date
    • Thread