Saving objects as favourites using Core Data and XML parsing
I am working on a project where the app parses data from an XML file, saves it to a persistent store and populates a table view. Every time the app opens, if there is internet connection available, the persistent store gets recycled and the XML is parsed again as it gets updated quite frequently. If no internet connection is available I'll just load the data from the latest parsing saved in my persistent store.
Once parsing is completed, users will then be able to browse through the objects and possibly mark them as favourites.
When this is done, I would like to be able to display these "marked as favourite" objects in a separate tab bar and keep them for as long as the user has them marked as favourites.
This could be done easily if the data from my persistent store wasn't being recycled so frequently but since that's not the case, I'd like to find the best way to:
- Create an a开发者_JS百科dditional persistent store;
- Be able to copy objects from one store to another
Or
Can you suggest a better way of implementing this?
Many thanks,
Rogerio
You can juts flag them as favorite and then don't delete or update them in the model. There is no need to copy the objects anywhere for something as simple as this.
Put a boolean flag in the entity called "favorite" and then if that flag is set to YES, don't update that entity on the next XML refresh and don't delete it.
For anyone interested in the solution to this problem, it turns out Marcus recommendation was spot on and way easier to implement than I initially anticipated.
The basic logic flow to make this work is:
Download XML feed from web service
Do a Fetch Request of objects flagged as favourite from persistent store
Implement logic to compare uniqueID from new object being parsed and the ones saved as favourites.
If a match is found, delete the existing/saved objected, continue with the parsing of the new one and mark it as favourite (this is only required if your object is likely to have additional/updated data otherwise simply ignore the object being parsed and keep to original one to save you the additional processing time).
Use NSFetchedResultsController to manage updating the views for you. I personally have a separate view for my favourites and use a predicate to only display objects marked as favourite.
Cheers, Rog
i don't know if you're problem is still up to date, but i tend to use an additional flag for your update list like marcus already explained. so you only need to compare the flags, which is done with a high performance compared two separate lists. especially you'll get a problem with you're memory if you keep every data twice.
精彩评论