Reputation: 8042
I am creating instances of an NSManagedObject in a nil managed object context as I don't want the objects to be saved unless the user chooses to later on.
I do this by creating the entity description with a context and then create the object itself without a context. This is shown below.
NZVideo *video = nil;
if ([[tempVideo objectForKey:_kResponseVideoTypeKey] isEqualToString:_kVideoTypeYouTube] == YES)
{
NSEntityDescription *entityDescription = [NZYouTubeVideo entityDescriptionInContext:[NSManagedObjectContext contextForCurrentThread]];
video = (NZVideo *)[[NSManagedObject alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:nil];
[(NZYouTubeVideo *)video setVideoId:[media objectForKey:_kResponseVideoIdKey]];
}
else if ([[tempVideo objectForKey:_kResponseVideoTypeKey] isEqualToString:_kVideoTypeS3] == YES)
{
NSEntityDescription *entityDescription = [NZS3Video entityDescriptionInContext:[NSManagedObjectContext contextForCurrentThread]];
video = (NZVideo *)[[NSManagedObject alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:nil];
[(NZS3Video *)video setAbsoluteVideoUrl:[media objectForKey:_kResponseVideoUrlKey]];
}
[video setTitle:title];
[video setSummary:summary];
[video setDurationValue:duration];
if (tempThumbnails != nil)
{
NSString *smallThumbnail = [tempThumbnails objectForKey:_kResponseVideoThumbnailSmallKey];
if (smallThumbnail != nil)
{
NSEntityDescription *entityDescription = [NZThumbnail entityDescriptionInContext:[NSManagedObjectContext contextForCurrentThread]];
NZThumbnail *thumbnail = (NZThumbnail *)[[NSManagedObject alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:nil];
[thumbnail setAbsoluteImageUrl:smallThumbnail];
[thumbnail setAbsoluteSizeValue:NZThumbnailSizeSmall];
[video addThumbnailsObject:thumbnail];
}
}
if (video != nil)
{
[videos addObject:video];
}
Then, when I want to save the objects, I add them to a managed object context. I also add all the objects the object has relationships to.
[MagicalRecord saveUsingCurrentThreadContextWithBlock:^(NSManagedObjectContext *context) {
id _video = nil;
if ([video isKindOfClass:[NZYouTubeVideo class]])
{
_video = (NZYouTubeVideo *)video;
}
else if ([video isKindOfClass:[NZS3Video class]])
{
_video = (NZS3Video *)video;
}
if (((NSManagedObject *)_video).managedObjectContext == nil)
{
[context insertObject:_video];
}
for (NZThumbnail *thumbnail in ((NZVideo *)_video).thumbnails)
{
[context insertObject:thumbnail];
}
[playlist addVideosObject:video];
}
completion:^(BOOL success, NSError *error) {
if (error != nil)
{
DDLogVerbose(@"%@", error);
}
}];
However, when restarting the app, the objects have been added to Core Data but all of the properties on the objects are null
.
Does anyone know why this happens and how I can make it store the properties? As said, the objects exists. They are persisted but all of the properties are null.
I don't think it makes any difference but I use MagicalRecord to create, save and retrieve entities.
Upvotes: 3
Views: 2795
Reputation: 77631
Don't do this. I've answered this same question before :-)
You should either:
Save all the required values and variables and then when the user presses "save" create and insert the managed object and save context.
Or:
Create and insert the object into the context. Keep a reference to the object and update them as the user enters information. If the user presses save then save context. If the user presses cancel then just delete the object.
You shouldn't be creating a managed object without inserting it into a context. It doesn't really make sense.
See this answer also iPhone Core Data: Initializing Managed Object without a context
Upvotes: 3