3

I am receiving the following response from my REST Server.

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

To which I am trying to map the following model:

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

and "expertUsers" is a set of objects, based of a class called "User".

Now, I am trying to follow the same approach I have used in the past to connect users to objects, without having the full object. I have been using the following solution, and it works great for single objects.

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

However, how can I replicate the same thing, but this time with an array of IDs?

I would need something like this:

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

EDIT #1:

Here is my current Core Data model:

@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];

Here is my Rest JSON answer for Cell:

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

However, I keep getting the following:

*** 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
4

1 回答 1

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" }];

假设您的实体是PictureandUser并且该User实体具有命名的身份属性userID,并且Picture实体具有命名的NSArrayorNSSet属性users并包含从usersJSON 中映射的身份。

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


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

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

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

  1. 添加属性managerIds
  2. 添加映射以填充managerIdsJSON 中的 id
  3. 更新cellManagersConnection参考managers
于 2013-08-31T20:33:24.237 回答