Magical Records将JSON导入Core Data

时间:2015-02-06 17:26:34

标签: ios objective-c json core-data magicalrecord

我说下面有核心数据实体:

Event.h

@interface Event : NSManagedObject

@property (nonatomic, retain) NSDate * event_date;
@property (nonatomic, retain) NSNumber * event_id;
@property (nonatomic, retain) NSString * event_name;
@property (nonatomic, retain) NSString * event_status;
@property (nonatomic, retain) NSSet *event_persons;
@property (nonatomic, retain) Store *store;

@end

Store.h

@interface Store : NSManagedObject

@property (nonatomic, retain) NSNumber * store_id;
@property (nonatomic, retain) NSString * store_name;
@property (nonatomic, retain) NSString * store_type;
@property (nonatomic, retain) NSSet *events;
@end

Event_person.h

@interface Event_person : NSManagedObject

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *event_person_id;
@property (nonatomic, retain) Event *event;
@property (nonatomic, retain) Person_type *person_type;

@end

Person_type.h

@interface person_type : NSManagedObject

@property (nonatomic, retain) NSString *person_type_id;
@property (nonatomic, retain) NSString *type;
@property (nonatomic, retain) Event_person *event_person;

@end

如果我从API接收JSON:

{
    "event_id": "12345",
    "event_name": "Demo",
    "event_date": "March 24 2015 12:00AM",
    "store": {
        "store_id": "789",
        "store_name": "Costco",
        "store_type": "Warehouse"
    },
    "event_person": [
        {
            "event_id": "580814",
            "name": "John",
            "person_type": {
                "person_type_id": "abc123",
                "type": "Demonstrator"
            }
        },
        {
            "event_id": "580814",
            "name": "Jane",
            "person_type": {
                "person_type_id": "cdf345",
                "type": "Helper"
            }
        }
    ]
}

在我的完成版块中,我有NSDictionary *resultDict = /// result from JSON parser然后将返回值导入我的Event NSManagedObject

Event *imported Event = [Person MR_importFromObject:resultDict];

问题:

1)假设没有基于返回的id存在Event,Store,Event_person或Person_type对象,魔法记录会在返回JSON的层次结构中创建所有对象,并且还会填充像

这样的属性 NSSet *event_persons and Store *store对象

Event NSSet *events对象

Store 对于NSSet *events对象

Person_type *person_typeEvent_person 对于Event_person *event_person对象,

Person_type

2)假设某些物体已经存在,魔法记录会更新物体的属性而不是创建新物体吗?

谢谢,

0 个答案:

没有答案