Arshad
Arshad

Reputation: 43

Inconsistency in Saving/Fetching Data in Core Data

My first post here, getting straight to the point.

I'm facing a problem and I'm not sure whether is with saving of data or fetching it. Here are my codes where I tried to save the data in my Entity, "Sequence". The entity consists of 2 attributes "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();
        }
    }

When printed, the arrays will always display the contents of the array.

This is where I try to fetch the data.

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);
}

The problem comes when I restart the application. The arrays either show (null) for both arrays or it shows the contents of both of the arrays. The if statement for if(![context save:&error]) never gets triggered. Subclasses of NSManagedObject for the entity has already been added.

I've also tried declaring the AppDelegate in @interface and forced to save the context immediately by doing [delegate saveContext];.

Here is the method where the saving happens. "check" is initialized to 0 at the viewDidLoad method. Both "arrForWk1" & "arrForWk2" are declared at @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];
}

Just to add on, the arrays are of data type NSMutableArray and I'm trying to store them into attributes of type "Transformable".

Upvotes: 2

Views: 199

Answers (1)

Arshad
Arshad

Reputation: 43

After researching around I managed to solve the problem. I'm not sure how it actually works but I solved it by rearranging the code when I'm trying to fetch the data.

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);
    } 

I've tried also tried 2 ways of saving the data. In the if statement where I tried to save the data, I converted the NSMutableArrays to NSArrays.

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();
        }
    }

The second way I tried is by using an answer from 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];

Apparently it worked somehow.

Upvotes: 1

Related Questions