Reputation: 440
I have a very small, lightweight application that uses core data to store arrays as NSData. I've read that this is bad practice as it will get bloated in the long run. But what are the alternatives? Is it better to create a plist and store the data on the disk?
Currently I am not seeing any dramatic performance hits to my app but I want to nip this in the bud before I eat my hat.
Upvotes: 1
Views: 617
Reputation: 8918
If you decide to stick with Core Data
If your arrays are filled with Foo objects, then create a Core Data entity called Foo. Give the Foo entity a displayOrder attribute that keeps track of its index in a hypothetical array.
When your app needs to display all the Foo objects in a table view, fetch the Foo objects from Core Data as follows:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Foo" inManagedObjectContext:self.context]];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"displayOrder" ascending:YES];
[request setSortDescriptors:@[sort]];
NSArray *results = [self.context executeFetchRequest:request error:NULL];
The disadvantage to this approach is that if you change the order of a single Foo object in the array, you might have to update the displayOrder of all the Foo objects in Core Data. But you can get clever about this and minimize such updates. For example, when adding a new Foo object to the array, add the Foo object to the end of the array; that way, no Foo objects in Core Data require updating with respect to displayOrder.
Upvotes: 1
Reputation: 1897
You are right, it is a completely bad approach to store data as Core Data gives you an advanced Object Oriented Storage, try mentally defining the type of data you are storing and structure a data model based on that.
If you have considered this approach but are not sure if you'll be able to structure it correctly , I recommend you starting with this CoreData tutorial http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started
Upvotes: 1