使用Mantle与核心数据 - NSSet和NSArray

Ely*_*ium 7 core-data objective-c ios github-mantle

我需要你的帮助,因为我无法理解这一点.我在iOS中使用Mantle和CoreData.

我的关系定义如下:

帖子1:N评论

当我从我的REST服务中提取数据时,我创建了一个Mantle对象帖子,其中包含一个NSMutableArray of Comments.这完美无瑕.

然后我将其存储在Core Data中,这是我不知道我是否正确行事的地方.

[MTLManagedObjectAdapter managedObjectFromModel:post insertingIntoContext:[self getManagedObjectContext] error:&error];
Run Code Online (Sandbox Code Playgroud)

所以我这样做是为了将我的帖子对象存储到Core Data中.核心数据模型具有称为"post_has_comments"的关系,这是一种级联的一对多关系.所以在对象帖子上我有"posts_has_comments" - >级联,在我的对象"评论"上我与"Nullify"有一对一的关系.

Afaik,Core Data将其视为NSSet.我试图放入的是一个NSMutableArray,因为Mantle会照顾这个(至少那是它的源代码中的快速看起来告诉我的).

不幸的是,当我从Core Data获取对象时

Post* post = [MTLManagedObjectAdapter modelOfClass:Post.class fromManagedObject:[[self fetchedResultsController] objectAtIndexPath:indexPath] error:nil];
Run Code Online (Sandbox Code Playgroud)

对post对象的属性注释是一个空的NSSet,并且在事先插入事物时我遇到了一些错误.我得到的错误:

Core Data: annotation: repairing missing delete propagation for to-many relationship post_has_comments on object [...]
Run Code Online (Sandbox Code Playgroud)

我被卡住了 - 也许我错过了一些巨大的东西?

My Post Class实现以下静态方法:

+ (NSDictionary *)managedObjectKeysByPropertyKey {
return @{
         @"post_id" : @"id",
         @"comments" : @"post_has_comments"
         };
}

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"post_id" : @"id",
             };
}

+ (NSDictionary *)relationshipModelClassesByPropertyKey {
    return @{
             @"comments" : IHComment.class
             };
}
Run Code Online (Sandbox Code Playgroud)

Mun*_*ndi -5

来自地幔文档:

Mantle 使您可以轻松地为 Cocoa 或 Cocoa Touch 应用程序编写简单的模型层。

这只是一个未经证实的说法。看着这个框架,我看不出证据在哪里。您应该获取对象,并使用 Apple 的 API 将它们插入到 Core Data 中。

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;
}
Run Code Online (Sandbox Code Playgroud)

这里的所有都是它的。