0

When i am using the NSPredicate while fetch the unique user data from Coredata Entity the app getting crash. But, no error logs. This is the code am using,

if (managedObjectContext == nil) 
{ 
    managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
}


NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Name==%@",selectedUserName];  
NSFetchRequest *fectchreq = [[NSFetchRequest alloc] init];
NSEntityDescription *entitydes = [NSEntityDescription entityForName:@"ALLData" inManagedObjectContext:managedObjectContext];
[fectchreq setEntity:entitydes];
[fectchreq setPredicate:predicate];

NSSortDescriptor *sortdes = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
NSArray *sortdesarray = [[NSArray alloc] initWithObjects:sortdes, nil];
[fectchreq setSortDescriptors:sortdesarray];
[sortdes release];
[sortdesarray release];   
NSError *error = nil; 
//NSMutableArray *storeddata = [[[managedObjectContext executeFetchRequest:fectchreq] error:&error] mutableCopy];
NSArray *storeddata = [managedObjectContext executeFetchRequest:fectchreq error:&error];
//[fectchreq release];
NSLog(@"StoreData : %@, count : %d",storeddata, [storeddata count]);

Can anyone please help to solve this problem? THanks in advance.

Gopinath
  • 5,392
  • 21
  • 64
  • 97
  • 2
    The second parameter to `executeFetchRequest:error:` is a pointer-to-a-pointer of error object. You should pass it with ampersand like this: `... executeFetchRequest:request error:&error]`. – Eimantas Jul 03 '12 at 07:36
  • @Eimantas Yes i have tried your words in my code. Still the app getting crash. Can you please help me? THanks. – Gopinath Jul 03 '12 at 07:45

1 Answers1

0
NSError *error; 
NSMutableArray *storeddata = [managedObjectContext executeFetchRequest:fectchreq error:error];

You need to change this code to:

NSError *error = nil;
NSArray *storeddata = [managedObjectContext executeFetchRequest:fectchreq error:&error];

Reasons: The error argument should be passed as a pointer because the declaration is -executeFetchRequest:(NSFetchedRequest *)request error:(NSError **)error; Also this method returns NSArray, not NSMutableArray - you must be getting compiler warning about this...

graver
  • 15,183
  • 4
  • 46
  • 62
  • Yes i have changed the code as per your advice. Still the app getting crash in the executeFetchRequest: line. Could you please help me? I will post the recent code in question. Thanks. – Gopinath Jul 03 '12 at 07:42
  • No errors in log. When the app come to the executeFetchRequest: line the app crashed. No error logs. Can you please help me? Thanks. – Gopinath Jul 03 '12 at 07:47
  • There is no way to help you without providing me some error. And there is no way you're not getting an error, at least you should see the signal which is a start... The posted code is correct. – graver Jul 03 '12 at 07:50
  • Ya the posted code is currently am working on. sharedlibrary apply-load-rules all (gdb) This is only am getting in my log. Please help me. Thanks. – Gopinath Jul 03 '12 at 07:51
  • Yes thanks a lot Mr.Graver. Really this is my mistake. I put a wrong column name in NSPredicate. Thanks a lot for your helping hand. – Gopinath Jul 03 '12 at 07:54
  • Is the attribute correct: `Name` and the entity name: `ALLData` – graver Jul 03 '12 at 07:54
  • I changed the attribute name in my code. Now it is working fine. Can you please help me to delete a selected person record from the entity? Please see this question and help me.http://stackoverflow.com/questions/11305590/how-to-get-unique-values-retrieve-from-coredata-iphone-app. Thanks – Gopinath Jul 03 '12 at 07:56