I have a CoreData entity Tracker which stores the dates.
The app receives a notification and the CheckListViewController enters data in CoreData for up to 13 days, so when the CheckListViewController gets dismissed, the CoreData entity Tracker will be filled with 13 rows.
In the MainViewController (which dismisses CheckListViewController), I have the following code:
- (void)dataSaved {
self.checkListVC dismissViewControllerAnimated:YES completion:^{
// fetching all the data from 'Tracker' entity and doing NSLog on it
// all data gets logged in console without any issues
}];
}
Now, after that somewhere in my code, I fetch all the data from the entity Tracker but the return data is empty. The CoreData doesn't show any error it simply returns and empty array.
Edit:
Code to fetch results from CoreData
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:ENTITY];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"date" ascending:NO];
[request setSortDescriptors:#[sortDescriptor]];
request.predicate = (fromDate && toDate) ? [NSPredicate predicateWithFormat:#"date >= %# AND date <= %#", fromDate, toDate] : nil;
__block NSArray* fetchedHabits;
[managedObjectContext performBlockAndWait:^{
NSError *error = nil;
fetchedHabits = [managedObjectContext executeFetchRequest:request error:&error];
if (error) NSLog(#"Unknown error occurred while fetching results from CoreData, %# : %#", error, [error userInfo]);
}];
CoreData model:
Update 1:
So as you can see there are two entities, namely Habit and Tracker. When I fetch results from Habit it all works fine, but when I try to fetch results from Tracker it gives me an empty array. I have a common NSManagedObjectContext instance because you can manage multiple CoreData entities with single managedObjectContext.
I have checked managedObjectContext.persistentStoreCoordinator.managedObjectModel.entitiesByName and it also lists both the entities.
Update 2:
Code where I add data in to Tracker
TrackerCoreData *tracker = [NSEntityDescription insertNewObjectForEntityForName:ENTITY
inManagedObjectContext:managedObjectContext];
tracker.date = date;
tracker.habits = habits;
// saving CoreData explicitly
NSError *error = nil;
[managedObjectContext save:&error];
There could be many reasons for your failure to display the records:
data was not saved
data was not retrieved correctly
data was not displayed correctly
All of these could be potentially complicated scenarios, but you should check them in this order.
A much better approach: use NSFetchedResultsController for your main view controller and have the delegate methods take care of updating your table view. No need to fetch, no work to be done in any completion methods - just save the data and the FRC will update your table.
Edit: how to check the physical database
It is possible that your data only exists in memory but is not actually saved to the database. Find the actual database file (in the documents folder of the app from the Simulator) and check it with the sqlite3 command line utility, or with the Firefox plugin "SQLite Manager".
Edit2: more concrete recommendations
You should make sure that you call:
[managedObjectContext save:&error];
Also double-check what your ENTITY macro stands for (not a very smart name).
It seems to me that you are overusing the block methods to no apparent purpose. First try to make everything work on the main thread (one context!). Only if you get performance problems consider background threads and context and calls to performBlock etc.
Related
I have a relatively simple entity. When I create it, set its attributes, and save it, it saves successfully. I can later retrieve it and it is not nil and I get a successful save message from MagicalRecord.
When I retrieve it and try to access any attribute though the attribute is nil. The entity itself is fine but the attributes are all nil. I have checked they are all definitely set correctly before I save.
I haven't encountered this problem before. Why could it be occurring?
NB: This doesn't happen every time. Most times I call the method to create and save this entity it can later be retrieved without any issues. The problem is intermittent but possible to replicate on every run.
Code:
Entity1 *entity1 = [Entity1 MR_createEntityInContext:localContext];
[entity1 setUpEntity:myobject];
EntityChild *entityChild=[EntityChild MR_createEntityInContext:localContext];
[entityChild setUpEntityChild:entity.child withContext:localContext];
[entityChild setEntity1:entity1];
[localContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
}];
Update:
If I look in the sqlite database and search for the entity it actually doesn't exist at all. So MagicalRecord tells me it saves, CoreData lets me retrieve a non-nil object (albeit with nil attributes) but no record exists in the database.
I did not understand ur code standards. As I am new to IOS Development. I Used below code for retrieving.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entityRef = [NSEntityDescription entityForName:#"Entity1" inManagedObjectContext:appDelegate.managedObjectContext];//localContext
[fetchRequest setEntity:entityRef];
NSError *error=nil;
NSArray *detailsArray = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (error) {
NSLog(#"Unable to execute fetch request.");
NSLog(#"%#, %#", error, error.localizedDescription);
}
Saving the data
NSManagedObjectContext *context = [appDelegate managedObjectContext];//localContext
NSManagedObject *objectRef = [NSEntityDescription
insertNewObjectForEntityForName:#"Entity1" inManagedObjectContext:context];
[objectRef setValue:#"IOS" forKey:#"Name"];
[objectRef setValue:#"positive" forKey:#"Attitude"];
NSError *error;
if (![context save:&error]) {
NSLog(#"Whoops, couldn't save: %#", [error localizedDescription]);
}
Hope it helps you...!
Ok, I got to the bottom of this. It wasn't a problem with the code when I did the save. It was actually a problem with some code in another class that was retrieving the data from the wrong context. When I changed the context it worked correctly.
I'm still not sure why this only happened occasionally and not every time the code was run but it's working now.
Thanks for your help anyway everyone.
In my app, I use Core Data foundation to store my data (some images). I need to fetch images from my data base and display them in a table view. When my app is loading, it will fetch some images from database. However, I am quite confused that sometimes the method
[context executeFetchRequest: request error: &error]
would fail to fetch data from my database while sometimes it works well (I am quite sure the data is indeed in the database). I add some test code here:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:#"Album" inManagedObjectContext:context]];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"number" ascending:YES];
request.sortDescriptors = #[sortDescriptor];
NSArray *matches = [context executeFetchRequest:request error:&error];
for (int i = 0; i < 10; i++) {
if ([matches count]) {
break;
}
matches = [context executeFetchRequest:request error:&error];
NSLog(#"try one more fetch for my data");
}
If the database is empty, the loop will exist after trying 10 times. But when my database is not empty, from NSLog method, I find that sometimes it fetched data successfully after trying 2 times and sometimes after 5 times, and sometimes after trying 10 times it still failed to fetch data. To be more specific, this phenomenon only happens when the first few [context executeFetchRequest: request error: &error] get executed. After that, the method works well.
So I want to know why does this happen, can anyone help me?
Your question is very generic. You should add details (what do you mean with fail, share your model, share the fetch request, etc)
Few tips here.
First of all executeFetchRequest:error: takes an error as param. So you have check it for errors.
NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
// log error here
} else {
// log results here
}
Second, if you are using a SQLite persistent store you can see logs Core Data creates in the following way: XCode4 and Core Data: How to enable SQL Debugging.
Finally, if you are using a SQLite persistent you can also inspect saved data. A possible path could be:
/Users/NAME/Library/Application Support/iPhone
Simulator/VERSION/Applications/APP ID/Documents/DB FILE.sqlite
In addition to Dan Shelly's comment, are you performing any background operation to display images?
I have seen this link where countforfetch is always 1. But doesn't give the solution.
When i do a fetch request as given in the link it gives me the data i was about to save every time. So since its already present it wont re-save. So the database is empty. But surprisingly the data comes on the table.
This seems like a very weird behaviour. Can some please help ?
here is my code
NSFetchRequest *fetchRequest12 = [[NSFetchRequest alloc] init];
NSError *error;
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"orderNumber = %#",orderList.orderNumber];
[fetchRequest12 setPredicate:predicate];
[fetchRequest12 setEntity:[NSEntityDescription entityForName:#"OrderList" inManagedObjectContext:appDelegate.managedObjectContext]];
NSLog(#"The fetch was successful! %#", [appDelegate.managedObjectContext executeFetchRequest:fetchRequest12 error:&error]);
if ([appDelegate.managedObjectContext countForFetchRequest:fetchRequest12 error:&error] ==0 ) { // Somethings to be done
}
Use setIncludesPendingChanges: to NO if you want the fetch request to ignore any changes that you have made in the MOC but not yet saved. By default all unsaved changes are fetched (hence you see unsaved changes displayed in your UI).
Having a really hard time grasping the way core data works, and I'm hoping I can get some very basic help here.
I have two entities:
Profiles<-->>Events
I have successfully figured out how to add profiles, view profiles in table view and view events for a profile in a table view via a predicate fetch.
Now, here is where I am lost. Lets say I want to update an event in the Event entity. Do I have to do a fetch with predicate to create a Profiles object before I update the Event entity? Or can I just update the Event entity and somehow tell it which Profile it is associated with via the relationship?
Here is where I have hit the log jam:
// add new event
//NSLog(#"Adding New Event");
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:#"Events"];
NSPredicate *predicate=[NSPredicate predicateWithFormat:#"ANY profile.profilename=[cd] %#",[self profilename]];
[fetchRequest setPredicate:predicate];
self.events = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
//insert event info
NSManagedObject *eventInfo = [NSEntityDescription insertNewObjectForEntityForName:#"Events" inManagedObjectContext:self.managedObjectContext];
///////// THIS IS WHERE I NEED HELP
}
// save the context
NSError *error = nil;
if (![managedObjectContext save:&error]){
NSLog(#"Error! %#",error);
}
I'm about ready just to create a flat file and work with that! It's driving me nuts!
EDIT - CHANGED CODE BELOW ***********************
// add new event
//NSLog(#"Adding New Event");
Events *newEvent = (Events *)[NSEntityDescription insertNewObjectForEntityForName:#"Events" inManagedObjectContext:managedObjectContext];
newEvent.eventdesc=self.eventDescTextField.text;
NSString *wkst = eventDescTextField.text;
NSNumber *wk = [NSNumber numberWithInteger: [wkst integerValue]];
newEvent.weeksout = wk;
So now I know I need to tell the Event entity to use the current profile..how do I access the relationship?
Looking at the code you've provided, I think you have a misconception about Core Data.
It looks like you are trying to get all the events related to a profile. You don't need to create ond perform a fetch request for this. Core Data is an object graph. Which means that if you have an object in an a managed object context, you get its related objects via it's relationships, you don't need to run a fetch request.
Three Questions but they are all related. If you like I can divide them into three questions so that you can more credits. Let me know if you'd like for me to do that.
I have the following code that allows me to access NSManagedObject
self.managedObjectContext = [(STAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSError *error;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:#"LetsMeet" inManagedObjectContext:managedObjectContext]];
NSArray *objectArray = [managedObjectContext executeFetchRequest:request error:&error];
if(objectArray.count==0){
letsMeet = (LetsMeet *) [NSEntityDescription insertNewObjectForEntityForName:#"LetsMeet" inManagedObjectContext:managedObjectContext];
} else{
letsMeet = (LetsMeet *)[objectArray objectAtIndex:0];
}
The code above allows me to save and retrieve attributes. i.e. I can access letsMeet.attribute to save and fetch.
Question 1: How do I delete and start a brand new managedObjectContext. i.e. User has a form that he's been filling out between the scenes. Everything is saved to CoreData from each scene as the user hits the Next button on the navigation Controller. After going through several screens, the user wants to cancel the form. At this point I would like to delete everything that has been saved thus far. Code example please.
Question 2: Lets say the user gets towards to end of the form and decides to save the form for later retrieval. How do I save a copy of the entire form as one object in Core Data. Code example please.
Question 3: How do I retrieve that saved object from Core Data at a later time and display what all the user had saved? Code example please.
To delete you just need to delete letsMeet object from NSManagedObjectContext.
NSError *error;
[managedObjectContext deleteObject:letsMeet];
[managedObjectContext save:&error];
Since you always have only one object, getting the reference of letsMeet is not a problem. You can do as you did in your code.
Update:
And you don't need to delete the managed object context. It just a space to deal with your objects. More explanation at the end of question.
2. If the LetsMeet entity is modeled in a way that all the form elements are attributes of LetsMeet, when you save the managedObjectContext after creating a LetsMeet object as you have done in code, this will be saved as a single object.
3.You already know how to retrieve an object as thats what you are doing in the code. Everything becomes easy as you are only using one object.
In the case of multiple objects to get a the unique object, you should either implement a primary key,(maybe formID, i.e; add another attribute to LetsMeet) or you should know what the objectId of each object is and then set the predicate of your fetch request accordingly.
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:letsMeet];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"formId like %#", formId];
[request setPredicate:predicate];
NSArray *resultsArray =[managedObjectContext executeFetchRequest:request error:&error];
If your formId is unique, this will return you a single object array.
But if you are using core-data for only handling one object, you could've used NSUserDefaults or write to a plist File to do this. This is kind of overkill.
Update:
To get the objectId of a NSManagedObject:
[letsMeet objectId];
ManagedObjectContext is like a whiteboard. The object you have inside the array, the object inside managed object context, its all the same. You can change the objects, add object, delete object etc. Only thing is whatever is the current state of the object(s) when you do a [managedObjectContext save:] , that is written to disk.