Using the mantle with master data - NSSet and NSArray

I need your help with something because I can't think it over. I use Mantle with CoreData on iOS.

I have defined relationships that look like this:

Message 1: N Comment

When I retrieve data from my REST service, I create a message to a Mantle Object that has NSMutableArray comments. It works flawlessly.

Then I save this to Core Data, and it is there that I do not know if I am doing the right thing.

[MTLManagedObjectAdapter managedObjectFromModel:post insertingIntoContext:[self getManagedObjectContext] error:&error]; 

So, I am doing this to save the post object in Core Data. The master data model has a relationship called "post_has_comments", which is a cascading one-to-many relationship. So, on the Post object, I have "posts_has_comments" โ†’ cascading, on my Comment object I have a one-to-one relationship with "Nullify".

Afaik, Core Data views this as an NSSet. What I'm trying to include is NSMutableArray, although Mantle will take care of it (at least this is what the quick story said in its source).

Unfortunately, when I return an object from Core Data using

 Post* post = [MTLManagedObjectAdapter modelOfClass:Post.class fromManagedObject:[[self fetchedResultsController] objectAtIndexPath:indexPath] error:nil]; 

The comments on the property of the post object are an empty NSSet, and I get quite some errors when pasting things in advance. Errors I get:

 Core Data: annotation: repairing missing delete propagation for to-many relationship post_has_comments on object [...] 

I'm stuck - Maybe I'm missing something huge here?

My Post Class implements the following static methods:

 + (NSDictionary *)managedObjectKeysByPropertyKey { return @{ @"post_id" : @"id", @"comments" : @"post_has_comments" }; } + (NSDictionary *)JSONKeyPathsByPropertyKey { return @{ @"post_id" : @"id", }; } + (NSDictionary *)relationshipModelClassesByPropertyKey { return @{ @"comments" : IHComment.class }; } 
+7
ios objective-c core-data github-mantle
source share
2 answers

From the Mantle documentation:

Mantle makes it easy to create a simple model layer for your Cocoa or Cocoa Touch application.

This is simply an unproven statement. Looking at the framework, I do not see where the evidence is. You need to get your objects and paste them into the underlying data using the Apple API.

 Post *cdPost = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext]; // configure the cdPost object with the data from the web service for (id commentObject in commentArrayFromPostObject) { Comment *cdComment = [NSEntityDescription insertNewObjectForEntityForName:@"Comment" inManagedObjectContext:self.managedObjectContext]; // configure the cdComment object with the data from the web service cdComment.post = cdPost; } 

That's all.

-5
source share

A simple workaround is to create your own method for setting properties, and if the value is set by NSSet , then convert it to NSMutableArray before returning it to the ivar property.

For example:

 - (void)setComments:(NSMutableArray *)comments { if ([comments isKindOfClass:NSSet.class]) { _comments = [NSMutableArray arrayWithArray:[((NSSet *)comments) allObjects]]; } else { _comments = comments; } } 

I have done it myself quite a few times, and it works like a charm!

0
source share

All Articles