开发者

How to store Fetch results in Application's plist file?

I'm querying rows from my sqlite database using Fetch and loading the results into a NSMutableArray myDataArray. What I want to do is to store the values in myDataArray into my application's preferences plist file. But when I run the code below I get the following error:

[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '( ...(note: contains 50 rows of values) )' of class '__NSArrayM'.

I'm storing other NSMutableArrays in my plist file using the same method but cannot figure out a way to store the queried database results.

You might ask: why store the values, just requery the database when I need the values again? The reason is I'm pulling random rows from the database and I need to recall the rows exactly as they were originally randomized.

Any help is appreciated, lq

 ...
 fetchResults = [managedObjectContext executeFetchRequest:request error:&error];
 self.myDataArray = [NSMutableArray arrayWithArray:fetchResults];

 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
 [userDefaults setObject:self开发者_运维问答.myDataArray forKey:kFetchResultsArray];


The items in myDataArray are not property list objects, so they can't be automatically serialized to a plist.

From the NSUserDefaults class reference for setObject:forKey::

The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects. See “What is a Property List?” in Property List Programming Guide.

You may want to consider a mechanism other than user defaults for storing this data. From the Property List Programming Guide:

Many applications require a mechanism for storing information that will be needed at a later time. For situations where you need to store small amounts of persistent data—say less than a few hundred kilobytes—property lists offer a uniform and convenient means of organizing, storing, and accessing the data.

In some situations, the property-list architecture may prove insufficient. If you need a way to store large, complex graphs of objects, objects not supported by the property-list architecture, or objects whose mutability settings must be retained, use archiving. See Archives and Serializations Programming Guide for more information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜