Re: NSDictionary crash
Re: NSDictionary crash
- Subject: Re: NSDictionary crash
- From: Aaron Burghardt <email@hidden>
- Date: Sat, 25 Jul 2009 21:58:49 -0400
Why use Core Foundation? How about (written in Mail):
- (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
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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