I want a list of unique contacts that I've stored with core data.
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"Post" inManagedObjectContext:[self managedObjectContext]];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSDictionary *entityProperties = [entityDescription propertiesByName];
[request setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:#"contactID"]]];
[request setReturnsDistinctResults:YES];
NSError *error = nil;
NSMutableArray *retValue = [[[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy] autorelease];
The result is always the same with or without the setPropertiesToFetch, so I guess there is something wrong with it, but I can't figure out what it is.
Can someone help me?
Did you set your fetch result type to NSDictionaryResultType? The documentation says setPropertiesToFetch: only works when result type == NSDictionaryResultType
.n
Related
My requirement to get name,eMail,mobileNo,sum(amount) from a table by core data.
If I go for sql then my sql query looks like this:
select name,eMail,mobileNo,sum(amount) amt1 from T1 GROUP BY name,eMail,mobileNo;
Here I am geting the correct result.
The same I have to do with core data.
I can fetch name and SUM(amount) by providing GROUP BY name.I have provided the below code for that.
[request setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
I don't know how to add multiple attribute in Group by in core data. Can any one help me for doing that.
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:#"Entity1" inManagedObjectContext:context];
NSAttributeDescription* statusDesc = [entityDesc.attributesByName objectForKey:#"name"];
NSExpression *sumExpression = [NSExpression expressionForFunction:#"sum:" arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:#"totalAmount"]]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc]init];
[expressionDescription setName: #"sumTot"];
[expressionDescription setExpression: sumExpression];
[expressionDescription setExpressionResultType:NSDecimalAttributeType];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
[request setPropertiesToFetch:[NSArray arrayWithObjects:#"name",expressionDescription, nil]];
[request setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
[request setResultType:NSDictionaryResultType];
NSError *error = nil;
myArray = [context executeFetchRequest:request error:&error];
Thanks for giving direction to solution.
I Got the Solution. It was a silly mistake between arrayWithObject and arrayWithObjects
NSAttributeDescription* statusDesc = [entityDesc.attributesByName objectForKey:#"name"];
NSAttributeDescription* statusDesc1 = [entityDesc.attributesByName objectForKey:#"eMail"];
NSAttributeDescription* statusDesc2 = [entityDesc.attributesByName objectForKey:#"mobileNo"];
After that I passed all descriptions like this.
[request setPropertiesToGroupBy:[NSArray arrayWithObjects :statusDesc,statusDesc1, statusDesc2, nil]];
First off I have to say this website and its members are amazing in their responses. Most helpful. thank you.
Secondly, I am troubleshooting the following error while troubleshooting an iPhone/iPad app that I am working on:
NSFetchRequest autoreleased with no pool in place - just leaking.
the culprit code seems to be
NSFetchRequest *request = [[NSFetchRequest alloc] init];
found in this method
-(NSArray *)searchDatabase:(int)uniqueID withPredicate:(NSString *)pred{
NSLog(#": DetailViewController/searchDatabase() method...executing. ");
NSLog(#": DetailViewController/searchDatabase() pred = %#",pred); // returns ParentID
NSError *error = nil;
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Directory" inManagedObjectContext:self.managedObjectContext];
//: moved above line
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"%#=%#", pred, [NSNumber numberWithInt:uniqueID]]];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setReturnsObjectsAsFaults:NO];
[request setEntity:entity];
[request setPredicate:predicate];
//: get the session and update the record with the time it ended
NSArray *mutableFetchResults = [self.managedObjectContext executeFetchRequest:request error:&error];
NSLog(#": DetailViewController/searchDatabase() mutableFetchResults = %#",mutableFetchResults);
[request release];
[error release];
NSLog(#": DetailViewController/searchDatabase() method...terminating.");
return mutableFetchResults;
}
I am not sure if the release statements are properly set up, but even still I am unsure while the error shows up in the first place. I am a little new to objective c, and at compile time the application did not indicate any errors (the message shows up at runtime).
If anyone can help me out with this, it would be most appreciated.
thanks
Edward
Im trying to save a list of Device classes (a custom class) using Core Data and retrieve it. But After I save it, my query, which is VERY simple, doesnt return any records.
My call to save the records always returns YES and no errors:
BOOL resultOfSave = [managedObjectContext save:&err];
My predicate for searching by the property userLogin is like so:
- (NSArray *) devicesForUser:(NSString *)username
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Device" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"userLogin = %#", username];
[request setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"macAddress" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error = nil;
NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&error];
if (fetchResults == nil) {
// Handle the error.
NSLog(#"ERROR: nil fetch result array");
}
[request release];
NSLog(#"devicesForUser [%#] count %d", username, [fetchResults count]);
return fetchResults;
}
but I always get zero results, if I get rid of the predecate, I get all the objects, and if I loop through them I can see that the userLogin property for at least one of the object IS set to the username that I pass in...
Has anyone ever had an issue like this???
Thanks for any help you can give
Mark
In your predicate, change:
#"userLogin = %#"
...to:
#"userLogin == %#"
I have completed and reproduced Core Data tutorials using a tableview to display contents. However, I want to access an Entity through a fetch on a view without a tableview. I used the following fetch code, but the count returned is always 0. The data exists when the database is opened using SQLite tools.
NSManagedObject *entryObj;
XYZDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Quote" inManagedObjectContext:managedObjectContext];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"id" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[request setEntity: entity];
NSArray *results = [managedObjectContext executeFetchRequest:request error:nil];
if (results == nil) {
NSLog(#"No results found");
entryObj = nil;
}else {
NSLog(#"results %d", [results count]);
}
[request release];
[sortDescriptors release];
count returned is always 0; it should be 5.
Can anyone point me to a reference or tutorial regarding creating a controller not to be used with a tableview.
As far as I can tell, everything looks fine (except I'm a little worried about using 'id' as an attribute name, since it's a keyword in Objective-C). Try removing the sort descriptor and see if that changes anything. Also, add an error check. Perhaps something is happening that that can tell you. And I assume "Quote" is the right name for the entity. If none of that helps, I don't think the problem is in the fetch request.
For what it's worth, here's how I would write it:
XYZDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = appDelegate.managedObjectContext;
NSFetchRequest * request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Quote" inManagedObjectContext:context];
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"id" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
[request setEntity:[NSEntityDescription entityForName:#"Quote" inManagedObjectContext:context]];
NSError * error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
[request release];
if (error) {
NSLog(#"ERROR: %# %#", [error localizedDescription], [error userInfo]);
}
if (results == nil) {
NSLog(#"No results found");
entryObj = nil;
}
else {
NSLog(#"results %d", [results count]);
}
I've got data in an NSSet, and I need to get it into an NSArray.
Do I need to sort it myself (again, this came from Core Data) or can I get it out in a sorted order?
You can specify a sort when you retrieve data with an NSFetchRequest by setting the sortDescriptors property to an array of NSSortDescriptors. But if you already have it in an NSSet and don't want to make another fetch request, you can use:
[[theSet allObjects] sortedArrayUsingDescriptors:sortDescriptors];
It'll create an interim NSArray when you call allObjects and then another sorted array afterwards, so it's not 100% efficient, but the overhead should be negligible for reasonably-sized data sets (and certainly less than the cost of sorting).
Edit
Actually, I was wrong - NSSet has the sortedArrayUsingDescriptors: method too. So you can just call [theSet sortedArrayUsingDescriptors:descriptors] and do it all in one go.
You've got to sort it yourself, but it's not very hard...
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Sprocket" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"sortVariable" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
self.sprocketArray = mutableFetchResults;
[sortDescriptors release];
[sortDescriptor release];
[mutableFetchResults release];
[request release];