我在我的Mac应用程序中使用了核心消息传递应用程序接口,它通过AFIncrementalStore使用核心数据。我有一个与Users实体相关的两种不同方式的Channel实体。这里有一个“所有者”关系,它很简单,而且运行良好。但是还有两个ACL实体:通道的读取器和写入器。
ACL只是一个包含用户is数组的键值对象,这是我不确定如何使用AFIncrementalStore来处理的关系。
我正在拉取一个Channel实体,它有一个附加的ACL对象("writers"),其中包含一个用户ID数组:
"writers": {
"any_user": false,
"immutable": true,
"public": false,
"user_ids": [
"1",
],
"you": true
},我已经在核心数据中设置了我的关系("writerUsers“,与用户建立了一种多对多的关系),但我在弄清楚在自动指纹识别系统中该在哪里配置它时遇到了麻烦。
我尝试过实现- (NSDictionary *)representationsForRelationshipsFromRepresentation:(NSDictionary *)representation ofEntity:(NSEntityDescription *)entity fromResponse:(NSHTTPURLResponse *)response,但这似乎只有在服务器响应包括实际的对象值- the整个用户实体,而不仅仅是ID的情况下才有效。
我还看到有人提到使用- (NSURLRequest *)requestWithMethod:(NSString *)method pathForRelationship:(NSRelationshipDescription *)relationship forObjectWithID:(NSManagedObjectID *)objectID withContext:(NSManagedObjectContext *)context来提供获取用户对象的URL请求……但是这个方法永远不会从我的AFHTTPClient子类中调用。
那么,当我只有一个ID时,如何教AFIncrementalStore引入一个用户实体呢?
发布于 2013-10-04 22:14:19
我能够解决我的问题。API的json格式应该是这样的:
"users": [
{"id":1},
{"id":2}
]即使API不提供这种格式的数据,您也可以在AFRESTClient的子类中“伪造”它。
- (NSDictionary *)representationsForRelationshipsFromRepresentation:(NSDictionary *)representation
ofEntity:(NSEntityDescription *)entity
fromResponse:(NSHTTPURLResponse *)response {
NSMutableDictionary *mutableRelationshipRepresentations = [[super representationsForRelationshipsFromRepresentation:representation ofEntity:entity fromResponse:response] mutableCopy];
if ([entity.name isEqualToString:@"writer"]) {
NSArray *user_ids = [representation objectForKey:@"user_ids"];
NSMutableArray *users = [[NSMutableArray alloc] init];
for (NSNumber *id in user_ids) {
[users addObject:@{@"id":id}];
}
}
[mutableRelationshipRepresentations setObject:users forKey:@"users"];
return mutableRelationshipRepresentations;
}当然,您需要在model.When中将"users“定义为多对多关系。在relationship对象中只有id,AFIncrementalStore会自动获取各个关系对象。
https://stackoverflow.com/questions/17960911
复制相似问题