使用Magical Record添加到创建的实体属性

时间:2013-10-23 05:52:56

标签: ios objective-c core-data magicalrecord

所以我在我的项目中使用魔法记录,我正在尝试执行以下操作:

- (void)persistNewReadingWithOneA:(NSString *)oneA oneB:(NSString *)oneB{

    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];

    FinalRead *reading1A = [FinalRead MR_createInContext:localContext];

    reading1A.a1 = oneA;
    reading1A.b1 = oneB;


    [localContext MR_saveOnlySelfAndWait];


}

正如您所看到的,我坚持使用新条目,现在我需要通过添加它的属性来更新相同的条目。我尝试了以下操作:

- (void)updateReadingWithTwoA:(NSString *)twoA twoB:(NSString *)twoB{

    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];

    FinalRead *reading2A = [FinalRead MR_createInContext:localContext];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname ==[c] %@ AND lastname ==[c] %@"];
    FinalRead *finalRead = [FinalRead MR_findFirstWithPredicate:predicate inContext:localContext];

    reading2A.a2 = twoA;
    reading2A.b2 = twoB;


    [localContext MR_saveOnlySelfAndWait];


}

任何想法?

编辑:

我从JSON响应中获取数据,如下所示:

- (void)fetchedData:(NSData *)responseData {

    if (self.buttonPressed){

        //parse out the json data
        NSError* error;
        NSArray* json = [NSJSONSerialization
                              JSONObjectWithData:responseData

                              options:kNilOptions
                              error:&error];

       NSString *string = [NSString stringWithFormat:@"%@", [json objectAtIndex:0]];

        [self persistNewReadingWithOneA:string
                                   oneB:nil];


    }else{

        //parse out the json data
        NSError* error;
        NSArray* json = [NSJSONSerialization
                              JSONObjectWithData:responseData

                              options:kNilOptions
                              error:&error];

        NSString *string = [NSString stringWithFormat:@"%@", [json objectAtIndex:0]];

        [self persistNewReadingWithOneA:nil
                                   oneB:string];



    }

}

然后存储到新创建的对象,如下所示:

- (void)persistNewReadingWithOneA:(NSString *)oneA oneB:(NSString *)oneB{

    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];

    FinalRead *reading1A = [FinalRead MR_createInContext:localContext];

    reading1A.a1 = oneA;
    reading1A.b1 = oneB;


    [localContext MR_saveOnlySelfAndWait];


}

ViewController然后推送到另一个问题,另一个问题是再次解析和存储更多JSON,但是进入刚刚在前一个ViewController中创建的SAME对象。我在NSManagedObject中具有以下属性PER对象:

@interface FinalRead : NSManagedObject

@property (nonatomic, retain) NSString *a1;
@property (nonatomic, retain) NSString *b1;
@property (nonatomic, retain) NSString *a2;
@property (nonatomic, retain) NSString *b2;
@property (nonatomic, retain) NSString *a3;
@property (nonatomic, retain) NSString *b3;
@property (nonatomic, retain) NSString *a4;
@property (nonatomic, retain) NSString *b4;

@end

所以基本上,每次我更改一个新的viewController时,我都想添加到同一个对象,直到使用了最终的View。

1 个答案:

答案 0 :(得分:1)

您的更新方法还会创建一个新对象:

FinalRead *reading2A = [FinalRead MR_createInContext:localContext];
// ... other stuff
reading2A.a2 = twoA;
reading2A.b2 = twoB;

此处提取的对象:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname ==[c] %@ AND lastname ==[c] %@"];
FinalRead *finalRead = [FinalRead MR_findFirstWithPredicate:predicate inContext:localContext];

完全未使用。事实上,我希望获取崩溃的请求,因为 谓词使用%@格式,不提供任何参数。

更新对象,您必须先获取它。要获取它你需要一些 标识要更新的对象的属性。例如:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname ==[c] %@ AND lastname ==[c] %@",
       firstName, lastName];
FinalRead *finalRead = [FinalRead MR_findFirstWithPredicate:predicate inContext:localContext];
if (finalRead) {
    finalRead.a2 = twoA;
    finalRead.b2 = twoB;
    // ... save context ...
} else {
    // no matching object found
}

更新:您的案例中的解决方案可能是从一个传递finalRead对象 查看控制器到下一个,而不是在每个视图控制器中重新获取它。