在我的核心数据模型中,我有两个实体:
InventoryItem具有以下属性:
属性:
@property (nonatomic, retain) NSNumber * allowsActions;
@property (nonatomic, retain) NSNumber * assetID;
@property (nonatomic, retain) NSNumber * inventoryObjectID;
@property (nonatomic, retain) NSString * objectDescription;
@property (nonatomic, retain) NSNumber * quantity;
@property (nonatomic, retain) NSNumber * retired;
@property (nonatomic, retain) NSString * serialNumber;
关系:
@property (nonatomic, retain) NSSet *action;
InventoryAction具有以下属性 属性:
@property (nonatomic, retain) NSDate * actionDate;
@property (nonatomic, retain) NSNumber * actionID;
@property (nonatomic, retain) NSString * actionLongValue;
@property (nonatomic, retain) NSString * actionShortValue;
@property (nonatomic, retain) NSString * notes;
@property (nonatomic, retain) NSNumber * userActionID;
@property (nonatomic, retain) NSString * userAuthorizingAction;
@property (nonatomic, retain) NSString * userPerformingAction;
@property (nonatomic, retain) NSNumber * userPerformingActionExt;
@property (nonatomic, retain) NSNumber * inventoryObjectID;
关系
@property (nonatomic, retain) InventoryItem *object;
现在您已掌握了所有这些信息,我将告诉您,CoreData模型中存储的所有值都来自JSON格式的Web服务。所有值都正确存储。
我遇到的问题是,当我从CoreData获取数据时,它只会带回唯一的值。
例如: 最后一个InventoryItem有6个与之关联的操作:
由于1和2具有相同的值,因此它仅检索值1,3,4,5和6.
以下是我用于获取数据的代码:
- (void)loadDetails
{
_fetchRequest = [[NSFetchRequest alloc] init];
_entity = [NSEntityDescription entityForName:@"InventoryObject" inManagedObjectContext:[self managedObjectContext]];
_sort = [NSSortDescriptor sortDescriptorWithKey:@"inventoryObjectID" ascending:YES];
_sortDescriptors = [[NSArray alloc]initWithObjects:_sort, nil];
[_fetchRequest setEntity:_entity];
[_fetchRequest setSortDescriptors:_sortDescriptors];
NSError *error;
_fetchedObjects = [[self managedObjectContext] executeFetchRequest:_fetchRequest error:&error];
for (InventoryItem *inventoryItem in _fetchedObjects) {
NSLog(@"Object Description: %@", [inventoryItem valueForKey:@"objectDescription"]);
NSLog(@"Object ID: %@", [inventoryItem valueForKey:@"inventoryObjectID"]);
InventoryAction *action = (InventoryAction *)inventoryItem.action;
NSLog(@"Action Long Value: %@", [action valueForKey:@"actionLongValue"]);
NSLog(@"Notes: %@", [action valueForKey:@"notes"]);
}
}
以下是JSON返回:
[{"MediaInventoryObjectsId":1,"AssetId":15,"Quantity":1,"SerialNumber":"R45DFL5","Description":"Test Camera 1","AllowActions":true,"Retired":false,"Actions":[{"MediaInventoryActionsId":3,"MediaInventoryObjectsId":1,"UserPerformingActionExt":4444,"UserActionId":1,"ActionDate":"2014-05-19T15:31:45.6","UserPerformingAction":"myersb","UserAuthorizingAction":"mccroskeyl","Notes":null,"ActionShortValue":"OUT","ActionLongValue":"Check Out"},{"MediaInventoryActionsId":4,"MediaInventoryObjectsId":1,"UserPerformingActionExt":4444,"UserActionId":2,"ActionDate":"2014-05-19T15:31:45.6","UserPerformingAction":"myersb","UserAuthorizingAction":"mccroskeyl","Notes":"everything looks good","ActionShortValue":"IN","ActionLongValue":"Check In"}]},{"MediaInventoryObjectsId":2,"AssetId":15,"Quantity":2,"SerialNumber":"IDKMYBFFJILL","Description":"Vanilla Ice Cream","AllowActions":true,"Retired":false,"Actions":[]},{"MediaInventoryObjectsId":3,"AssetId":15,"Quantity":1,"SerialNumber":"R2D23P0","Description":"Droid Bee Box","AllowActions":true,"Retired":false,"Actions":[{"MediaInventoryActionsId":5,"MediaInventoryObjectsId":3,"UserPerformingActionExt":4444,"UserActionId":1,"ActionDate":"2014-05-20T14:48:29.53","UserPerformingAction":"myersb","UserAuthorizingAction":"mccroskeyl","Notes":null,"ActionShortValue":"OUT","ActionLongValue":"Check Out"},{"MediaInventoryActionsId":6,"MediaInventoryObjectsId":3,"UserPerformingActionExt":4444,"UserActionId":2,"ActionDate":"2014-05-20T14:48:29.58","UserPerformingAction":"myersb","UserAuthorizingAction":"mccroskeyl","Notes":"everything looks good","ActionShortValue":"IN","ActionLongValue":"Check In"},{"MediaInventoryActionsId":7,"MediaInventoryObjectsId":3,"UserPerformingActionExt":5555,"UserActionId":1,"ActionDate":"2014-05-20T14:48:29.6","UserPerformingAction":"farmer","UserAuthorizingAction":"mccroskeyl","Notes":null,"ActionShortValue":"OUT","ActionLongValue":"Check Out"},{"MediaInventoryActionsId":8,"MediaInventoryObjectsId":3,"UserPerformingActionExt":5555,"UserActionId":2,"ActionDate":"2014-05-20T14:48:29.6","UserPerformingAction":"farmer","UserAuthorizingAction":"mccroskeyl","Notes":"its all slimy now","ActionShortValue":"IN","ActionLongValue":"Check In"},{"MediaInventoryActionsId":9,"MediaInventoryObjectsId":3,"UserPerformingActionExt":6666,"UserActionId":1,"ActionDate":"2014-05-20T14:48:29.61","UserPerformingAction":"intern1","UserAuthorizingAction":"mccroskeyl","Notes":"it looks kinda slimy","ActionShortValue":"OUT","ActionLongValue":"Check Out"},{"MediaInventoryActionsId":10,"MediaInventoryObjectsId":3,"UserPerformingActionExt":6666,"UserActionId":2,"ActionDate":"2014-05-20T14:48:29.62","UserPerformingAction":"intern1","UserAuthorizingAction":"mccroskeyl","Notes":"oh god....why would you do that to an inanimate object you sicko","ActionShortValue":"IN","ActionLongValue":"Check In"}]}]
我在某处读到NSSet可能只会带回唯一记录。这是真的,如果是这样,那么关系的解决方案是什么:action是NSSet?
答案 0 :(得分:1)
您的关系声明如下:
@property (nonatomic, retain) NSSet *action;
但你这样访问它:
InventoryAction *action = (InventoryAction *)inventoryItem.action;
这是不正确的。 action
关系是包含NSSet
实例的InventoryAction
,它本身不是InventoryAction
。即使您将其分配给InventoryAction *
,您实际拥有的是NSSet
。然后你这样做:
NSLog(@"Notes: %@", [action valueForKey:@"notes"]);
如果您在valueForKey:
上调用NSSet
,则会返回该组中对象的该键的所有唯一值。在这种情况下,它会返回集合中notes
个对象的InventoryAction
属性的所有唯一值,这就是您所看到的。如果要查找每个实例而不是每个唯一值,则需要以下内容:
NSSet *actions = inventoryItem.action;
for (InventoryAction *action in actions) {
NSLog(@"Action note: %@", action.note);
}
[警告,以上内容只是输入我的网页浏览器....]