IOS 8 - Creating Temporary NSManagedObjects

In iOS 7 (and earlier), it became possible to effectively create a “temporary” NSManagedObject with the ability to later add it to the context and save it like this:

 NSEntityDescription* entityDescription = [NSEntityDescription entityForName:@"User" inManagedObjectContext:managedObjectContext]; User* user = [[User alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:nil]; 

Note the nil NSManagedObjectContext parameter. (Mark Marcus S. Zarra's answer to this method here )

However, iOS 8 changed the relationship management, so if you create a temporary object and add a relation to it before setting its context, the connection will be deleted after the restart:

 User* user = [User temporaryEntity]; [user addPhotosObject:photo]; [managedObjectContext:insertObject:user]; [managedObjectContext:&error]; 

This does not affect non-relational objects, but makes it impossible to create temporary objects that have relationships.

Does anyone know how to account for this change and create / use temporary, working NSManagedObject s? Thanks!

-

Also check out this related post on the iOS 8 forum.

+5
source share
1 answer

create temporary objects in a temporary context, and also extract your relationships into this temporary context

use MOC as a notepad and save it or don’t save the context at the end

that's what i did forever

+5
source

Source: https://habr.com/ru/post/1200760/


All Articles