Simon
Simon

Reputation: 25983

Can I make instances of an NSManagedObject in no particular NSManagedObjectContext?

I'm building an app which receives a number of listings from a web API, and allows the user to save some for offline viewing. My usual approach would be:

  1. Get the data from the API, and make a new Listing object for each datum
  2. Save the object to the DB if the user chooses to do so.

But this is a Core Data app, so the context is what gets saved, not the object. Under those circumstances, the above would become something like this:

  1. Get the data from the API, and make an unmanaged Listing object for each datum
  2. Move the object into the managed context if the user chooses to do so, then save the context

One approach to having an unmanaged and a managed version of Listing objects would be to have two classes, e.g. ManagedListing and UnmanagedListing - but that's a horribly repetitive way of doing it.

What I'd like is to make Listing a subclass of NSManagedObject; initialise a bunch of them without an NSManagedObjectContext; then when I want to save one, I either set its context or I copy its attributes to a new Listing inside a context.

Can I make instances of an NSManagedObject in no particular NSManagedObjectContext? If so, how?

Upvotes: 2

Views: 616

Answers (2)

Rob Napier
Rob Napier

Reputation: 299345

Use two persistent stores, one in memory and one on disk. If the user wants to save, move the object to the other store using assignObject:toPersistentStore:.

Upvotes: 7

Nekto
Nekto

Reputation: 17877

In iOS < 5.0 - yes. In iOS >= 5.0 - no.

Upvotes: 0

Related Questions