在我的核心数据模型我有两个实体:CoreData和实体关系问题
- InventoryItem
- InventoryAction
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可能只会带回唯一记录。这是真的吗?如果是这样的话,解决方案与关系是什么:行动是NSSet?
谢谢
bnjmnmyers
这做到了。你是男人!谢谢,汤姆! –