将addConnectionForRelationship用于ID数组

时间:2013-08-21 19:56:31

标签: ios objective-c ios6 restkit

我从REST服务器收到以下响应。

{
    id: 1,
    model_id: 1,
    plant_id: 1,
    users: [
       2,
       9
    ]
}

我试图映射以下模型:

@property (nonatomic, retain) Model *model; 
@property (nonatomic, retain) Plant *plant; 
@property (nonatomic, retain) NSSet *expertUsers;

和“expertUsers”是一组对象,基于一个名为“User”的类。

现在,我尝试按照我过去使用的相同方法将用户连接到对象,而没有完整的对象。我一直在使用以下解决方案,它适用于单个对象。

[pictureMapping addConnectionForRelationship:@"lastModifiedBy" connectedBy:@{ @"lastModifiedByID": @"identifier" }];

但是,我怎样才能复制同样的东西,但这次使用了一系列ID?

我需要这样的东西:

[modelExperts addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"users" toKeyPath:@"expertUsers" withMapping:userMapping **addConnectionForRelationship:@"lastModifiedBy" connectedBy:@{ @"lastModifiedByID": @"identifier" }**]];

编辑#1:

这是我目前的核心数据模型:

@interface Cell : NSManagedObject
@property (nonatomic, retain) NSNumber *identifier;
@property (nonatomic, retain) NSSet *managers; // To Many Users
@end


@interface User : NSManagedObject
@property (nonatomic, retain) NSNumber *identifier;
@end

RKEntityMapping *cellMapping = [RKEntityMapping mappingForEntityForName:@"Cell" inManagedObjectStore:managedObjectStore];
cellMapping.identificationAttributes = @[@"identifier"];

NSEntityDescription *cellManagersEntityDescription = [NSEntityDescription entityForName:@"Cell" inManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
NSRelationshipDescription *userManagersRelationshipDescription = [cellManagersEntityDescription relationshipsByName][@"managers"]; // To many relationship for the `User` entity
RKConnectionDescription *cellManagersConnection = [[RKConnectionDescription alloc] initWithRelationship:userManagersRelationshipDescription attributes:@{ @"managers": @"identifier" }];
[cellMapping addConnection:cellManagersConnection];

这是我对Cell的Rest JSON答案:

{
id: 1,
managersIDs: [
2
],}

但是,我一直得到以下内容:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot connect relationship: invalid attributes given for source entity 'Cell': managers'
*** First throw call stack:
(
0   CoreFoundation                      0x03c646f4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x02df38b6 objc_exception_throw + 44
2   CoreFoundation                      0x03c64558 +[NSException raise:format:arguments:] + 136
3   Foundation                          0x029d45ee -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4   Poka                                0x001db61f -[RKConnectionDescription initWithRelationship:attributes:] + 1279
5   Poka                                0x0000bdf3 -[APIConnector setupMapping] + 12451
6   Poka                                0x000089ef -[APIConnector init] + 159
7   Poka                                0x001863da -[IJContext createClassInstanceFromRegistration:withProperties:] + 186
8   Poka                                0x00184f12 -[IJContext instantiateClass:withProperties:] + 594
9   Poka                                0x00186de0 +[NSObject(Injective) injectiveInstantiate] + 96
10  Poka                                0x00008275 -[PokaAppDelegate application:didFinishLaunchingWithOptions:] + 309
11  UIKit                               0x01b65525 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 309
12  UIKit                               0x01b65d65 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1536
13  UIKit                               0x01b6a578 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
14  UIKit                               0x01b7e57c -[UIApplication handleEvent:withNewEvent:] + 3447
15  UIKit                               0x01b7eae9 -[UIApplication sendEvent:] + 85
16  UIKit                               0x01b6c1f5 _UIApplicationHandleEvent + 736
17  GraphicsServices                    0x048bc33b _PurpleEventCallback + 776
18  GraphicsServices                    0x048bbe46 PurpleEventCallback + 46
19  CoreFoundation                      0x03bdfe95 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
20  CoreFoundation                      0x03bdfbcb __CFRunLoopDoSource1 + 523
21  CoreFoundation                      0x03c0a8ac __CFRunLoopRun + 2156
22  CoreFoundation                      0x03c09bf3 CFRunLoopRunSpecific + 467
23  CoreFoundation                      0x03c09a0b CFRunLoopRunInMode + 123
24  UIKit                               0x01b69cad -[UIApplication _run] + 840
25  UIKit                               0x01b6bf0b UIApplicationMain + 1225
26  Poka                                0x0000810d main + 141
27  libdyld.dylib                       0x032db725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:4)

您要找的班级是RKConnectionDescription。代码看起来像:

NSEntityDescription *pictureEntity = [NSEntityDescription entityForName:@"Picture" inManagedObjectContext:managedObjectContext];
NSRelationshipDescription *expertUsers = [pictureEntity relationshipsByName][@"expertUsers"]; // To many relationship for the `User` entity
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:expertUsers attributes:@{ @"users": @"userID" }];

假设您的实体是PictureUser,并且User实体具有名为userID的标识属性,而Picture实体具有NSArray实体1}}或NSSet属性,名为users,包含从JSON中users映射的标识。

然后,您可以将连接添加到pictureMapping


在新代码中,这一行是错误的:

RKConnectionDescription *cellManagersConnection = [[RKConnectionDescription alloc] initWithRelationship:userManagersRelationshipDescription attributes:@{ @"managers": @"identifier" }];

因为它使用了保存实体实例的managers属性。它应该链接到您没有的属性,该属性包含您要链接到的管理器实体实例的标识。所以:

  1. 添加属性managerIds
  2. 使用JSON
  3. 中的ID添加映射以填充managerIds
  4. 更新cellManagersConnection以引用managers
相关问题