Query interface for iPhone CoreData store - iphone

another iPhone newbie question...
I have the following:
NSPersistentStoreCoordinator
NSManagedObjectContext
NSManagedObjectModel
Is it possible to run queries directly on the store (since its a sqlite DB)? I'm trying to delete all the records from a tableview, and figured a "DELETE FROM table" would be nice and quick as opposed to looping through the records and removing them manually (which i'm also struggling with).
Thanks for your time,
James

Core data acts as a wrapper for the underlying data store, so it's not really a great idea to begin circumventing core data. Additionally, core data adds additional information to your DB, so directly accessing the DB may (or may in the future) cause problems.
To delete all records via core data, I have the following:
+ (void) deleteAll {
NSManagedObjectContext *managedObjectContext = [(myAppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:[[self class] description] inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
for (NSManagedObject *managedObject in items) {
[managedObjectContext deleteObject:managedObject];
NSLog(#"%# object deleted",[[self class] description]);
}
}

Related

Core Data. ExecuteFetchRequest with fault NSManagedObjects (not in RAM)

I need to execute fetch request. But when I do it I get not fault NSManagedObjects (each of the objects is about 5 Mb, that's why I get the memory warning). Apple provides faulting possibility for Core Data (when objects are not loaded in RAM). And I wanna my objects to use this possibility.
Here is my code
+ (NSMutableSet *)GetImagesWithPredicate:(NSPredicate *)predicate
{
NSString *entityName = kEntityName;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];;
NSManagedObjectContext *context = appDelegate.managedObjectContext;
NSEntityDescription *entityDesctiption = [NSEntityDescription
entityForName: entityName
inManagedObjectContext:context];
// find object
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesctiption];
[request setPredicate:predicate];
NSArray *objects = [context executeFetchRequest:request error:nil];
[request release];
if (objects == nil)
{
NSLog(#"there was an error");
return nil;
}
NSMutableSet *set = [NSMutableSet setWithArray:objects];
return set;
}
where predicate is (id < 500).
App crashes after
NSArray *objects = [context executeFetchRequest:request error:nil];
because all the data of objects appears in the RAM of iPhone.
It seems that default option returnsObjectsAsFaults = YES doesn't work.
The objects are probably being returned as faults; you can verify this with isFault. The issue is that Core Data automatically pre-fetches the property values for those objects and places them in the row cache (in memory). You can disable this behaviour by setting includesPropertyValues to NO on the NSFetchRequest object.
See the includesPropertyValues documentation for details of all this and the performance implications.
As an aside, you might not want to store lots of large objects in the database directly. You probably should look into using external storage if you're targeting iOS 5, or else using separate files yourself with their names/paths/ids in Core Data.
you could set the - (void)setResultType:(NSFetchRequestResultType)type for the NSFetchRequest and only get the relevant attributes for your Object with the -(void)setPropertiesToFetch:(NSArray *)values Method.
And only lazy loading the needed attributes.

Populating the Tableview from the Core Data

I have performed the save operation in core data and it is successfully done .It stores the data .I have also fetched the data into the log. This is my code for fetching data in log but I dont know how to fetch this data in TableView.
NSError *error;
DemoAppCoreDataAppDelegate *appdelegate = (DemoAppCoreDataAppDelegate *)[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [appdelegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName: #"Employee" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedobject = [context executeFetchRequest:fetchRequest error:&error];
tablearray = [[NSMutableArray alloc] initWithArray:fetchedobject copyItems:YES];
for (NSManagedObject *info in fetchedobject ) {
NSLog(# "%#",[info valueForKey:#"name"] );
}
[fetchRequest release];
I would recommend to use a NSFetchedResultsController.
Apple provides complete sample code in the NSFetchedResultsController documentation
The NSFetchedResultsController is specifically designed to work in between a tableView and Core Data. It makes everything a lot easier.
For example it will automatically insert and delete rows when you add or remove objects from the core data. For this you have to implement the NSFetchedResultsControllerDelegate protocol. The full sample code for this is in the protocol documentation
Use fetchedobject as dataSource to the tableView.
After [fetchRequest release]; , set the delegate and datasource for the tableView programatically.
The best way in this case is to inspect some working example I think. Look at the apple's example project Recipies, good one btw: easy and gives a good picture of how the things with CoreData and tableViews should be done:
http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008913

core data delete not work

I use the following code to populate a UITableView
self.navigationItem.leftBarButtonItem = self.editButtonItem;
test_coredataAppDelegate *appDelegate = (test_coredataAppDelegate *)[[UIApplication sharedApplication] delegate];
self._context = appDelegate.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"patients" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
NSError *error;
self.PatientsList = [_context executeFetchRequest:fetchRequest error:&error];
self.title = #"patients";
[fetchRequest release];
where PatientsList is NSArray
when I want to delete record I use
[self._context deleteObject:[_PatientsList objectAtIndex:indexPath.row]];
NSError *error;
if (![_context save:&error]) {
// Handle error
NSLog(#"Unresolved error series %#, %#", error, [error userInfo]);
}
[PatientsList removeObjectAtIndex:indexPath.row] ;
[self.tableView reloadData];
but this doesn't have any effect.
How can I solve this issue and delete successfully, especially when my model contain relations?
Here you delete it from db but not from array so you need to delete from array or call database for again load the arry with new db details.
so for first approach delete it from array make your array mutable and use
removeObjectAtIndex method for deleting the object
or in second approach you need to fetch data again from core data.
and then reload table by using
[table reloadData];
Could you post a little more information about the results you're getting from fetch requests before and after you call deleteObject:? You could also check the userInfo property of the NSError object that you pass to some Core Data functions as that often gives very help information in Core Data. Also remember that your modifications aren't "set in stone" until the data model is saved using the save: method of your NSManagedObjectContext instance.
The way Core Data handles the relationships of a deleted object is defined in your Core Data schema (.xcdata file), which you can edit in Xcode. You can set the delete action of relationships to "Deny", "Nullify", "Cascade" or "No Action" here. See the 'Relationship Delete Rules' section of the documentation found here.

Display CoreData Records

I create an application using Coredata. I create a XML file and store all data into XML file. Then using coredata I store them into Database.
Now I want to view the all records from core data. My application create a sqlite file. But if I use select command in sqlite, there is no records displayed. But it shows "no errors".
Is there any way to view my records from core data or from Sqlite?
I faced the same issue, and need to investigated the sqlite db created by coredata. I used the free Sqlite database browser. http://sqlitebrowser.sourceforge.net/
And browsed to the path where the sqlite file is.
display / List out all the objects currently in the database
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"UserInfo" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
NSLog(#"First Name: %#", [info valueForKey:#"firstname"]);
NSManagedObject *details = [info valueForKey:#"address"];
NSLog(#"Zip: %#", [details valueForKey:#"zip"]);
}

How to Determine if a Table Contains Any Records with Core Data?

I haven't seen any other questions quite like this on here, but I'm hoping someone has some insight. I'm just starting to learn Core Data.
Basically, I have two methods and I want to choose which one to call with an if/else statement based on whether or not the "Contacts" table contains any records. Is there a way using core data to check if there are any records in a table?
The best way I've found so far is to set the fetchLimit to 1 and then check to see if anything returns.
[request setFetchLimit:1];
But I keep thinking there has to be a better/easier way. Anyone know or have a good reference I can look at?
Thanks a ton!
Yes, definitely there is a better method. Setup a fetch request as usual, but, instead of actually executing it, simply ask for the number of objects it would have returned if it had been passed to executeFetchRequest:error:
This can be done using
- (NSUInteger)countForFetchRequest:(NSFetchRequest *)request error:(NSError **)error;
Something like this:
- (int) numberOfContacts{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSManagedObjectContext *managedObjectContext = yourManagedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Contacts" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSError *error = nil;
NSUInteger count = [managedObjectContext countForFetchRequest:request error:&error];
[request release];
if (!error){
return count;
}
else
return -1;
}
It's not necessarily any better or easier, but you can look for a specific record and then create it if it doesn't exist like this:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Contact"
inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSError *error;
// Filter based on a predicate
[fetchRequest setPredicate:
[NSPredicate predicateWithFormat:#"identifier == %#", #"1"]];
NSManagedObject *contact = [[managedObjectContext
executeFetchRequest:fetchRequest error:&error] lastObject];
// If the contact was not found
if (!contact)
{
// Create the contact
contact = [NSEntityDescription insertNewObjectForEntityForName:#"Contact"
inManagedObjectContext:managedObjectContext];
[contact setValue:[NSNumber numberWithInt:1] forKey:#"identifier"];
[managedObjectContext save:nil];
}
Marcus Zarra posted some code that demonstrates this in a feed reader app. Marcus is the Core Data master.