0

I'm sorry the title may mislead you, since I'm not so good at English. Let me describe my problem as below (You may skip to the TL;DR version at the bottom of this question).

  1. In Coredata, I design a Product entity. In app, I download products from a server. It return JSON string, I defragment it then save to CoreData.

  2. After sometimes has passed, I search a product from that server again, having some interaction with server. Now, I call the online product XProduct. This product may not exist in CoreData, and I also don't want to save it to CoreData since it may not belong to this system (it come from other warehouse, not my current warehouse).

  3. Assume this XProduct has the same properties as Product, but not belong to CoreData, the developer from before has designed another Object, the XProduct, and copy everything (the code) from Product. Wow. The another difference between these two is, XProduct has some method to interact with server, like: - (void)updateStock:(NSInteger)qty;

  4. Now, I want to upgrade the Product properties, I'll have to update the XProduct also. And I have to use these two separately, like:


id product = anArrayContainsProducts[indexPath.row];
if ([product isKindOfClass:[XProduct class]] {
    // Some stuff with the xproduct
}
else {
    // Probably the same display to the cell.
}

TL;DR

Basically, I want to create a scenario like this:

  1. Get data from server.
  2. Check existed in CoreData.
  3. 2 == true => add to array (also may update some data from server).
  4. 2 == false => create object (contains same structure as NSManagedObject from JSON dictionary => add to array.

The object created in step 4 will never exist in CoreData.

Questions

  1. How can I create an NSManagedObject without having it add to NSMangedObjectContext and make sure the app would run fine?
  2. If 1 is not encouragement, please suggest me a better approach to this. I really don't like to duplicate so many codes like that.

Update

I was thinking about inheritance (XProduct : Product) but it still make XProduct the subclass of NSManagedObject, so I don't think that is a good approach.

Eddie
  • 1,903
  • 2
  • 21
  • 46

1 Answers1

0

There are a couple of possibilities that might work.

One is just to create the managed objects but not insert them into a context. When you create a managed object, the context argument is allowed to be nil. For example, calling insertNewObjectForEntityForName(_:inManagedObjectContext:) with no context. That gives you an instance of the managed object that's not going to be saved. They have the same lifetime as any other object.

Another is to use a second Core Data stack for these objects, with an in-memory persistent store. If you use NSInMemoryStoreType when adding the persistent store (instead of NSSQLiteStoreType), you get a complete, working Core Data stack. Except that when you save changes, they only get saved in memory. It's not really persistent, since it disappears when the app exits, but aside from that it's exactly the same as any other Core Data stack.

I'd probably use the second approach, especially if these objects have any relationships, but either should work.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • With your answer + http://stackoverflow.com/a/3008439/1696598, I've found the solution quite nice! I choose to use your first approach, since it's easier to implement to my CoreDataUtil. – Eddie Jan 26 '16 at 10:16