views:

25

answers:

1

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];
A: 

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.

Robot K
Thank you for your quick response. Using an archive doesn't seem like a good solution for me, so what I'm considering is redesigning the app so that rather than pull all 50 rows of data into myDataArray, I will instead pull just the PK_IDs into an array, store them in the plist, then Fetch each row one at a time using the Predicate to call each PK_ID.
Lauren Quantrell