I have an app that uses Core Data
to manage Entities
that we call lists
. Each list has an offlineData
property which is of the type Binary Data
. The Allows External Storage
option is true for this.
This entity holds an array of custom objects using the following method:
NSArray *customObjects;// My custom objects that have been added
self.list.offlineData = [NSKeyedArchiver archivedDataWithRootObject:customObjects];
This will save the objects to a Core Data
blob since all the objects are are using initWithEncoder:
and encodeWithCoder:
.
When users then want to view the list we do the opposite:
NSArray *items = [NSKeyedUnarchiver unarchiveObjectWithData:self.list.offlineData];
The Problem
This seemed like a great solution at first (obviously stupid now) because we could easily add additional properties to the objects, etc. However what we didn't anticipate is users adding thousands of objects. This has caused:
- Memory problems and crashes when trying to archive or create an array of thousands of objects
- Memory problems when unarchiving the objects
- Depending on device speed unarchiving can take a very long time, leaving the user to sit and wait
- Limitations in the amount of objects the users can add to a list.
- Speed issues when searching objects since I can't do a fetch request.
Solution?
The solution I am thinking of would be to migrate all the custom objects to Core Data
entities. This would be a To Many
relationship between the list entity
and the object entity
.
This seems like it would solve the problems outlined above, but I am not 100% sure.
Does anyone have advice on this, and if this is the best way to "migrate"?