Core Data - NSFetchRequest Problem - iphone

I have a problem with my NSFetchRequest, it doesn't seem to be fetching any data, code below:
-(void)addAnnotations
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"WayPoint" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
//NSPredicate *predicate = [NSPredicate predicateWithFormat:#"waypoint_map_id=%i", 98];
//[request setPredicate:predicate];
NSError *error;
listArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
[request release];
NSLog(#"Array: ", listArray);
}

Problem solved:
NSLog(#"Array: %d", listArray);

Related

How to get the sm_owner User object using StackMob as backend

I find out the User has a sm_owner field. But how do I get the user using sm_owner. My code is like this:
[self.client getLoggedInUserOnSuccess:^(NSDictionary *result) {
NSString *currentLogginedUser = [result valueForKey:#"sm_owner"];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"User" inManagedObjectContext:self.managedOjbectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"sm_owner like user/%#", currentLogginedUser];
[request setPredicate:predicate];
self.user = [[self.managedOjbectContext executeFetchRequest:request error:nil] objectAtIndex:0];
} onFailure:^(NSError *error) {
}];
And the crash message is:reason: 'Unimplemented SQL generation for predicate (sm_owner LIKE user / "user/test")'
I'm the platform Evangelist for StackMob.
The result from the getLoggedInUserOnSuccess method normally contains primary key "username". So the following code should work.
[self.client getLoggedInUserOnSuccess:^(NSDictionary *result) {
NSString *currentLogginedUser = [result objectForKey:#"username"];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"User" inManagedObjectContext:self.managedOjbectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"username == %#", currentLogginedUser];
[request setPredicate:predicate];
self.user = [[self.managedOjbectContext executeFetchRequest:request error:nil] objectAtIndex:0];
} onFailure:^(NSError *error) {
}];
Alternatively, if you rather grab the sm_owner from the NSDictionary result. I would do the following.
NSString *sm_owner = [result valueForKey:#"sm_owner"];
NSArray *myArray = [sm_owner componentsSeparatedByString:#"/"];
NSString *currentLogginedUser = [myArray objectAtIndex:1];
Then perform your fetch.

iPhone:similar to "where" type clause in core data

I am using core data first time in my application for my problem I have done google a lot but I didn't find specific solution.
I have Entity:SubCategory
Attributes:mainCategoryId,subCategoryName.
Now I want to fetch the data like
Select subCategoryName from SubCategory where mainCategoryId=1;
The code which I have implemented is as follows:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"SubCategory" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SubCategoryName == %#",#"Triangular1"];
[request setPredicate:predicate];
NSError *error;
arrSubCategory = [managedObjectContext executeFetchRequest:request error:&error];
How can I fetch similar kind of data in coredata
share your ideas
thanx in advance.
From your question what you need is,
[NSPredicate predicateWithFormat:#"mainCategoryId = %#",#"1"];
So your code will look like,
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"SubCategory" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate =[NSPredicate predicateWithFormat:#"mainCategoryId = %#",#"1"];
[request setPredicate:predicate];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
NSLog(#"subCategoryName: %#", [info valueForKey:#"subCategoryName"]);
}
The above is just a sample code. Actual implementation could be different based on your requirement.
A tutorial on coredata is here. Check that out as well.
You can do it like this
NSString *maincategoryId = #"1";
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:#"SubCategory"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:#"rubriqueid == %#", maincategoryId]];
and after you call your NSMutableArray to put your result:
NSMutableArray *answensmuarray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

How to fetch unique field values (song_type) from core data

I would like to know how to fetch unique field values (song_type) from core data.
i have the below code for that
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Songs" inManagedObjectContext:myManagedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:#"song_type"]];
[fetchRequest setEntity:entity];
NSError *aError;
NSMutableArray *fetchedResultsArray = [[myManagedObjectContext executeFetchRequest:fetchRequest error:&aError]mutableCopy];
if(! fetchedResultsArray ){
NSLog(#"EEEERRROR");
}
else {
NSLog(#"fetched %d", [fetchedResultsArray count]);
}
[fetchedResultsArray release];
[fetchRequest release];
i am getting the below error
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'Invalid keypath song_type
passed to setPropertiesToFetch:'
Set following 2 properties and you are done.
fetchRequest.returnsDistinctResults = YES;
fetchRequest.resultType = NSDictionaryResultType;
Hope this helps.
EDIT
NSFetchRequest *fetchRequest= [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Songs" inManagedObjectContext:myManagedObjectContext];
//Take properties dictionary
NSDictionary *entityProperties = [entity propertiesByName];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:#"song_type"]]];
NSArray * result = [myManagedObjectContext executeFetchRequest:fetchRequest error:nil];

Record data to attributes in core data Iphone

I have about 20 entity : Event1 ... Event20
In each entity i have property values1 ... values20
I need record data in for example Event5 values8
But something wrong - may be data record in each values8 (Event1 ... Event20)
How do right?
NSString *str = [NSString stringWithFormat:#"Event%d", variable];
NSString *value = [NSString stringWithFormat:#"values%d", vari];
TermometrAppDelegate *app;
app = (TermometrAppDelegate *)[UIApplication sharedApplication].delegate;
NSFetchRequest *fetchRequests = [[NSFetchRequest alloc] init];
NSEntityDescription *entit = [NSEntityDescription entityForName:str inManagedObjectContext:app.managedObjectContext];
[fetchRequests setEntity:entit] ;
[fetchRequests setPropertiesToFetch :[NSArray arrayWithObject:value]];
[fetchRequests setReturnsDistinctResults:YES];
NSError *error;
NSArray *fetchedObject = [app.managedObjectContext executeFetchRequest:fetchRequests error:&error];
NSManagedObject *fetched = nil;
NSManagedObject *fetch = nil;
printf("\n%d", [fetchedObject count]);
The code below retrieves values in Entity5 for Value8 equal to someValueForEight.text (UITextField). This should help you out.
TermometrAppDelegate *app;
app = (TermometrAppDelegate *)[UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = [app managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:#"Entity5" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"(value8 = %#)", someValueForEight.text];
[request setPredicate:pred];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];

Fetch Data using predicate. Retrieve single value

I want to retrieve a single value (String) from "Entry." I believe I am missing something. Can someone point it out?
XYZAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"name == %#", entryToSearchFor];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Entry" inManagedObjectContext:managedObjectContext];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[request setEntity: entity];
[request setPredicate: predicate];
NSArray *results = [managedObjectContext executeFetchRequest:request error:nil];
if (results == nil) {
NSLog(#"No results found");
}else {
NSLog(#"entryToSearchFor %#", entryToSearchFor);
NSLog(#"results %#", [results objectAtIndex:0]);
}
Btw, the NSLog results outputs the following:
results <NSManagedObject: 0x3d2d360> (entity: Entry; id: 0x3d13650 <x-coredata://6EA12ADA-8C0B-477F-801C-B44FE6E6C91C/Entry/p3> ; data: <fault>)
NSFetchRequest will return an array of NSManagedObjects that matches the request. That is consistent with what you are seeing. Assuming you want to fetch the name, you can do NSLog(#"the name is %#", [[results objectAtIndex:0] name]); to print out the name.