Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why coredata is returning null array while I am inserting values in table

Tags:

ios

core-data

I have two methods,

In first method, I do save values in Core Data, while in other, I simply fetch them.

After inserting, when I fetch data in same method, it shows value, but when I try to fetch in other method if returns me null.

My saving Method is

-(void) saveloginData:(NSString *)facebookTok username:(NSString *)userName password:(NSString*)password flag:(NSString *)flag {


NSError *error;

NSManagedObjectContext *context = [self managedObjectContext];



NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"SignIn" inManagedObjectContext:context]];
[fetchRequest setIncludesPropertyValues:NO]; //only fetch the managedObjectID

NSString *facebookTokenData = facebookTok;
NSString *usernameData = userName;
NSString *passwordData = password;
NSString *flagData = flag;


NSLog(@"Facebook Token%@\nUsername%@\npassword%@\nflat%@\n",facebookTokenData,usernameData,passwordData,flagData);


SignIn *signIn = [NSEntityDescription
                                        insertNewObjectForEntityForName:@"SignIn"
                                        inManagedObjectContext:context];

signIn.facebookToken = facebookTokenData;
signIn.username = usernameData;
signIn.password = passwordData;
signIn.flag = flagData;


NSEntityDescription *entity = [NSEntityDescription entityForName:@"SignIn"
                                          inManagedObjectContext:context];

[fetchRequest setEntity:entity];

NSArray *fetchedArray = [context executeFetchRequest:fetchRequest error:&error];


for (SignIn *info in fetchedArray) { 
            \\ THis executes and shows values, proves that value are inserted.

    NSLog(@"Name  ~~~~ : %@", info.username);
    NSLog(@"Password ~~~~~~~~ :%@", info.password);
    NSLog(@"FLAG ~~~~~~~~~~~ %@",info.flag);
    NSLog(@"Facebook Token %@", info.facebookToken);
}
}

My retrieve Method is

-(NSArray*) getLoginData {

NSError *error;

NSManagedObjectContext *context = [self managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"SignIn"
                                          inManagedObjectContext:context];


[fetchRequest setEntity:entity];

NSArray *fetchedData = [[NSArray alloc] init];


fetchedData = [context executeFetchRequest:fetchRequest error:&error];

NSLog(@"The count of Array %d", [fetchedData count]); \\ HERE COUNT IS ZERO, WHY?


for (SignIn *info in fetchedData) {
    NSLog(@" FF    Name  ~~~~ : %@", info.username);
    NSLog(@"Password ~~~~~~~~ :%@", info.password);
    NSLog(@"FLAG ~~~~~~~~~~~ %@",info.flag);
    NSLog(@"Facebook Token %@", info.facebookToken);
}


return fetchedData;
 }

Please guide that where I am doing mistake.

like image 732
Duaan Avatar asked Oct 04 '22 13:10

Duaan


1 Answers

Your problem is that you need to save conext to get the entity it later.

NSManagedObjectContext save: Attempts to commit unsaved changes to registered objects to their persistent store.

- (BOOL)save:(NSError **)error

Parameters: error: A pointer to an NSError object. You do not need to create an NSError object. The save operation aborts after the first failure if you pass NULL.

Return Value YES if the save succeeds, otherwise NO.

So you need to save context after you modify your object:

signIn.facebookToken = facebookTokenData;
signIn.username = usernameData;
signIn.password = passwordData;
signIn.flag = flagData;

[context save:NULL]; // NULL if you don't need to handle error
like image 183
Tala Avatar answered Oct 12 '22 11:10

Tala