在核心数据中保存/获取数据不一致

时间:2017-05-31 08:10:18

标签: ios objective-c core-data

我的第一篇文章,直截了当。

我遇到了问题,我不确定是保存数据还是取出数据。 这是我的代码,我试图在我的实体中保存数据,“序列”。该实体由2个属性“seqForWk1CD”和&组成。 “seqForWk2CD”。

AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [delegate managedObjectContext];
SequenceMO *seqEntity = [NSEntityDescription 
insertNewObjectForEntityForName:@"Sequence" 
inManagedObjectContext:context];
// some other code
seqEntity.seqForWk1CD = arrForWk1;
seqEntity.seqForWk2CD = arrForWk2;
NSLog(@"%@", arrForWk1);
NSLog(@"%@", arrForWk2);
NSError *error = nil;
if (context != nil) {
    if ([context hasChanges] && ![context save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }

打印时,数组将始终显示数组的内容。

这是我尝试获取数据的地方。

AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [delegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sequence" inManagedObjectContext:context];
[request setEntity:entity];
NSError *error;
if(![context save:&error]){
    NSLog(@"Error fetching. %@ %@", error, [error localizedDescription]);
}
NSUInteger count = [context countForFetchRequest:request error:&error];
if(count != 0){
    NSArray *fetchObj = [context executeFetchRequest:request error:&error];
    NSManagedObject *sequence = (NSManagedObject *)[fetchObj objectAtIndex:0];
    NSLog(@"1 - %@", sequence);
    arrForWk1 = [sequence valueForKey:@"seqForWk1CD"];
    NSLog(@"%@", arrForWk1);
    arrForWk2 = [sequence valueForKey:@"seqForWk2CD"];
    NSLog(@"%@", arrForWk2);
}

当我重新启动应用程序时出现问题。数组要么显示(null)两个数组,要么显示两个数组的内容。 if(![context save:&error])的if语句永远不会被触发。已经添加了实体的NSManagedObject的子类。

我还尝试在@interface中声明AppDelegate并强制通过[delegate saveContext];立即保存上下文。

这是保存发生的方法。 “check”在viewDidLoad方法中初始化为0。两个“arrForWk1”& “arrForWk2”在@interface上声明。

- (IBAction)randomizeSequence:(UIButton *)sender { 
   NSMutableArray *storeArray = [[NSMutableArray alloc] init];
   AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
   NSManagedObjectContext *context = [delegate managedObjectContext];
   SequenceMO *seqEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Sequence" inManagedObjectContext:context];
   BOOL record = NO;
   int x;

   for (int i=0; [storeArray count] < 9; i++) //Loop for generate different random values
    {
       x = 1 + arc4random() % 9;//generating random number
       if(i==0)//for first time
       {
        [storeArray addObject:[NSNumber numberWithInt:x]];
       }
       else
       {
          for (int j=0; j<= [storeArray count]-1; j++)
          {
              if (x ==[[storeArray objectAtIndex:j] intValue])
                  record = YES;
          }

          if (record == YES)
          {
              record = NO;
          }
          else
          {
              [storeArray addObject:[NSNumber numberWithInt:x]];
          }
       }
    }
    check++;
    if(check == 1 ) {
       arrForWk1 = storeArray;
       [self.wk1Seq reloadData];
    }
    else if(check == 2) {
        arrForWk2 = storeArray;
        seqEntity.seqForWk1CD = arrForWk1;
        seqEntity.seqForWk2CD = arrForWk2;
        NSError *error = nil;
        if (context != nil) {
            if ([context hasChanges] && ![context save:&error]) {
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            }
        }

        [self.wk2Seq reloadData];
}

再加上,数组的数据类型为NSMutableArray,我试图将它们存储到“Transformable”类型的属性中。

1 个答案:

答案 0 :(得分:0)

经过研究,我设法解决了这个问题。我不确定它是如何工作的但我通过在我尝试获取数据时重新排列代码来解决它。

AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [delegate managedObjectContext];
NSEntityDescription *descriptor = [NSEntityDescription entityForName:@"Sequence" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
request.entity = descriptor;
NSError *error;
NSArray *fetchObj = [context executeFetchRequest:request error:&error];
if(fetchObj == nil) {
    NSLog(@"Error occured when trying to fetch.");

}
else {
    if(fetchObj.count == 0) {
        NSLog(@"No objects saved");
    else {
        NSManagedObject *sequence = (NSManagedObject *)[fetchObj objectAtIndex:0];
        NSLog(@"1 - %@", sequence);
        arrForWk1 = [sequence valueForKey:@"seqForWk1CD"];
        NSLog(@"%@", arrForWk1);
        arrForWk2 = [sequence valueForKey:@"seqForWk2CD"];
        NSLog(@"%@", arrForWk2);
        NSLog(@"2 - %@", sequence);
    } 

我也尝试了两种保存数据的方法。在我尝试保存数据的if statement中,我将NSMutableArray转换为NSArray s。

else if(check == 2) {
    test2 = storeArray;
    NSManagedObjectContext *context = [delegate managedObjectContext];
    SequenceMO *seqEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Sequence" inManagedObjectContext:context];
    NSArray *tArr = [arrForWk1 copy];
    NSArray *tArr2 = [arrForWk2 copy];
    seqEntity.seqForWk1CD = tArr;
    seqEntity.seqForWk2CD = tArr2;
    NSError *error = nil;
    if (context != nil) {
        if ([context hasChanges] && ![context save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }

我尝试的第二种方法是使用Core Data not saving changes to Transformable property

的答案
id temp = [seqEntity seqForWk1CD];
id temp2 = [seqEntity seqForWk2CD];
temp = arrForWk1;
temp2 = arrForWk2;
[seqEntity setSeqForWk1CD:temp];
[seqEntity setSeqForWk2CD:temp2];

显然它以某种方式起作用。