Basic data. Create several managed objects, but save them only?

I am trying to write a favorite system for my application. I have already converted my model to a managed entity. So, imagine that the user is presented with a screen with a list of such objects. They can choose to save some to their favorites, which will be saved in the master data.

The problem is that when I create all these model objects, I do this with the context of the managed object. If the user saves one of his favorites, he will save the entire context and save all the individual objects. Additional functions will not be in their favorites, since adding to favorites creates a "favorite" entity, which is saved and points to an object that others will not have. But all other objects will be useless.

What is the standard way out of this / standard way to design your favorite iPhone system? Should I separate my model into two classes, one of which I will show to the user, and then the one that is stored in db? That way, I could build my models without putting them in the MOC. But it will be a duplicated class with all the same fields.

+6
iphone core-data
source share
2 answers

There is actually no standard way to do this because Core Data expects you to save the objects you create. However, if you create objects using:

id object = [[NSManagedObject alloc] initWithEntityDescription:entity inManagedObjectContext:nil]; 

They will not have context to save. Then for those that you need to save, you can:

 [[self managedObjectContext] insertObject:object]; 

Then call -save: into the context, and only those that have their own set of contexts will be saved.

+6
source share

It would not be easier to have the isFavorite property for your managed objects. Then, in your favorite form, can you filter based on this?

0
source share

All Articles