我从我的 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
最佳答案
您要找的类(class)是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" }];
假设您的实体是 Picture
和 User
并且 User
实体有一个名为 userID
的标识属性Picture
实体有一个名为 users
的 NSArray
或 NSSet
属性,并包含从 映射的身份>users
在 JSON 中。
然后您可以将连接添加到您的pictureMapping
。
在您的新代码中,这一行是错误的:
RKConnectionDescription *cellManagersConnection = [[RKConnectionDescription alloc] initWithRelationship:userManagersRelationshipDescription attributes:@{ @"managers": @"identifier" }];
因为它使用保存实体实例的 managers
属性。它应该链接到一个您没有的属性,该属性包含您要链接到的管理器实体实例的标识。所以:
- 添加属性
managerIds
- 添加映射以使用 JSON 中的 ID 填充
managerIds
- 更新
cellManagersConnection
以引用managers
关于ios - 对 ID 数组使用 addConnectionForRelationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18366509/