UItableview not refreshing updated data from core data - objective-c

i have a uitableview with a method fetch list that runs every 5 secs. when there is updated data in the core data for that record, the fetch list method does not update the latest into its array. thus, when i reload the data, it always shows the "old" record.
i call this method, followed by a reload data on the uitableview.
this is my fetch list method:
- (void) fetchList:(int) listNo{
// Define our table/entity to use
self.marketWatchListArray = nil;
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Market_watch" inManagedObjectContext:context];
// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
Mobile_TradingAppDelegate *appDelegate = (Mobile_TradingAppDelegate *)[[UIApplication sharedApplication] delegate];
NSPredicate *getList = [NSPredicate predicateWithFormat:#"(list_id == %d) AND (userID == %#)", listNo, appDelegate.user_number];
[request setPredicate:getList];
NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:#"stockName" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortByName]];
// Fetch the records and handle an error
NSError *error;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];
if (!mutableFetchResults) {
// Handle the error.
// This is a serious error and should advise the user to restart the application
}
// Save our fetched data to an array
[self setMarketWatchListArray: mutableFetchResults];
[mutableFetchResults release];
[request release];
}
strange thing is the table does not update with the latest when the timer runs fetchlist:1. When i swap to fetchlist:2, then back to fetchlist:1, the table is updated with the latest.
I have a segmentcontrol to toggle between different list.

Add this line before the fetch:
[context setStalenessInterval: 4.0]; // allow objects to be stale for max of 4 seconds

At the end of fetchList method, you add below code to refresh data in UITableView :
[yourTableView reloadData];

Related

NSFetchResultsController sorting is not working when inserting new entity

I have an CoreData entity called "MyPhoto" with the attributes :- photoData, timeStamp, folderName. Setting the today Date with Time to timeStamp property when saving photoData to core data. I want sections with folderName and inside the section I want to sort the photos with timeStamp. I am using and NSFetchResultsController to fetch the data & UICollectionView to display it.
Problem is: When I try to insert the new photo, it is not inserting in the correct sorting order but when I relaunch the app, it will show the all the photos in a correct sorting order.
here is the code I am using:
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *moc = [[CoreDataManager sharedInstance] managedObjectContext];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"MyPhoto" inManagedObjectContext:moc];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Sort using the timeStamp property.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"folderName" ascending:YES];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:#"timeStamp" ascending:NO];
[fetchRequest setSortDescriptors:#[sortDescriptor, sortDescriptor1]];
// Use the folderName property to group into sections.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:moc sectionNameKeyPath:#"folderName" cacheName:#"Root"];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
I hope I have explained my problem clearly. Thanks in advance for your help.
I see you are setting the delegate. Have you actually implemented any of the delegate methods?
For starters, you could just call reloadData in controllerDidChangeContent:

Core Data not updating, updated model

I have a very weird problem that has stumped me rather!
I have a core data entity that i have just added some new attributes to:
deleted - Boolean
deletedDate - Date
I have the following code, that upon pressing sets both those values on the core data object:
- (IBAction)deleteButtonInTable:(id)sender {
//Get the ID of the currently selected item in the table
NSInteger selected = [self.tweetTableView rowForView:sender];
//Create a predicate and fetch the objects from Core Data
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:#"approved == NO"];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:#"postDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
[request setPredicate:testForTrue];
[request setSortDescriptors:sortDescriptors];
[request setEntity:[NSEntityDescription entityForName:#"Tweet" inManagedObjectContext:_managedObjectContext]];
//Setup the Request
[request setEntity:[NSEntityDescription entityForName:#"Tweet" inManagedObjectContext:_managedObjectContext]];
//Assign the predicate to the fetch request
NSError *error = nil;
//Create an array from the returned objects
NSArray *fetchedObjects = [_managedObjectContext executeFetchRequest:request error:&error];
Tweet *selectedTweet = [fetchedObjects objectAtIndex:selected];
if (selectedTweet) {
selectedTweet.deleted = [NSNumber numberWithBool:TRUE];
selectedTweet.deletedDate = [NSDate date];
NSLog(#"%#",selectedTweet);
[self refreshTableView];
if (! self.tweetTableView){
NSLog(#"Tableview doesn't exist!!)");
}
[[self tweetTableView] reloadData];
[[self managedObjectContext] commitEditing];
[self saveAction:nil];
}
if ([self.autoWriteTweets isEqualToString:#"YES"]){
[self writeTweetsToXML];
[self saveAction:nil];
}
}
Now, if i watch the object in xcode with some breaks, i can see the attribute change on the object as i pass through the function, but i have an Table displaying a datasource, which is filtered to only show objects that have the deleted bool set to true, and nothing ever shows up there.
Now, to make things even more confusing i have a function that exports an array of the objects:
-(void)writeTweetsToXML{
//Create new fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//Set new predicate to only fetch tweets that have been favourited
NSPredicate *filterFavourite = [NSPredicate predicateWithFormat:#"approved == YES"];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:self.exportSort ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
[request setSortDescriptors:sortDescriptors];
//Setup the Request
[request setEntity:[NSEntityDescription entityForName:#"Tweet" inManagedObjectContext:_managedObjectContext]];
[request setResultType:NSDictionaryResultType];
//Assign the predicate to the fetch request
[request setPredicate:filterFavourite];
NSError *error = nil;
//Create an array from the returned objects
NSArray *tweetsToExport = [_managedObjectContext executeFetchRequest:request error:&error];
NSAssert2(tweetsToExport != nil && error == nil, #"Error fetching events: %#\n%#", [error localizedDescription], [error userInfo]);
//NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//NSString *path = [NSString stringWithFormat:#"%#/tweets.xml", documents];
NSString *writeerror;
if(tweetsToExport) {
NSString * exportLocationFull = [[NSString alloc]initWithFormat:#"%#/tweets.xml",self.exportLocation];
BOOL success = [tweetsToExport writeToFile:exportLocationFull atomically:YES];
NSLog(#"Write Status = %d to %#", success, exportLocationFull);
}
else {
NSLog(#"%#",writeerror);
}
}
Now, when i look at the exported file, two things happen which are odd!
Firstly, an object that i have seen have it's deleted value set to true, exports with the value as 0.
Secondly, the deletedDate attribute does not export at all, every, despite it being in the core data model. I can't see any way this can happen as i am doing no specific filtering on the export.
It's like a getter/setter somewhere is broken, but i have checked the class files and everything is as it should be and set to #dynamic.
Any help would be greatly appreciated as i'm a bit lost as to what the hell is going on.
People had warned me about core data's quirks, but this is just plain odd!
Cheers
Gareth
Note 1
As an aside, i am using the exact same code from the first section to set other attributes on objects that are filtered and that seems to work fine!
You should not name an Core Data attribute "deleted", that conflicts with the
isDeleted method of NSManagedObject.
Compare https://stackoverflow.com/a/16003894/1187415 for a short analysis of that problem.
There are other attribute names that cause conflicts, e.g. "updated" (compare Cannot use a predicate that compares dates in Magical Record). Unfortunately, there are no warnings at compile time or runtime,
and the documentation on what acceptable attribute names are is also quite vague.
Things to check:
Did you save your core data entities with [managedObjectContext save:&error] at the appropriate places (e.g. before displaying the new table view data)? Did you check the error variable?
Did you migrate your model correctly with a new model version?
Are you reading the correct attributes and displaying them correctly (in UI or log statements)?
BTW, in your code you are setting the request entity twice.
Try saving the mananged object context before loading the table view.
The boolean deleted may be 0 before and not be changed or it may be auto-initialized (there is an field in the inspector to set default values) to 0. Date fields on the other hand are nil by default.
P.S. Use [NSNumber numberWithBoolean:YES] in Objective-C.

Core Data Edit/Save Attributes in an Entity

I am struggling with the editing/saving in Core Data and need some help in this. I am using NSFetchedResultsController and have an entity named Golfer with attributes- first_name, last_name, email_id and others in Core Data. So, I know how to add and remove golfers from the database.
I am working on one view controller called ViewManager (kinda base view for all my classes) and it has 2-3 Custom UIViews inside it. I animate them in and out whenever I need them.
I add a golfer to the tableview, then on didSelectRow tableview method, I present my edit View inside the same ViewManager controller and try to update the textfields in the edit view using the following code, but it's updating at random indexes in the tableview and not working for me. Any help would be greatly appreciated.
- (IBAction)saveEditGolfersView:(id)sender
{
AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = [applicationDelegate managedObjectContext];
// Retrieve the entity from the local store -- much like a table in a database
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Golfer" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// Set the sorting -- mandatory, even if you're fetching a single record/object
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:#"first_name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release]; sortDescriptors = nil;
[sortDescriptor1 release]; sortDescriptor1 = nil;
NSError * error;
NSArray * objects = [context executeFetchRequest:request error:&error];
for(int i = 0; i<[objects count]; i++)
{
Golfer * golfguy = [objects objectAtIndex:i];
golfguy.first_name = mEditFirstName.text;
golfguy.middle_name = mEditMiddleName.text;
golfguy.last_name = mEditLastName.text;
golfguy.email_id = mEditEmailField.text;
golfguy.contactNumber = mEditContactNum.text;
golfguy.picture = mEditPictureView.image;
NSLog(#"name-%#", golfguy.first_name);
}
[request release]; request = nil;
error = nil;
[context save:&error];
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut
animations:^ {
mEditGolfersView.frame = CGRectMake(-480, mEditGolfersView.frame.origin.y, mEditGolfersView.frame.size.width, mEditGolfersView.frame.size.height);
}
completion:^(BOOL finished) {
mEditGolfersView.hidden = YES;
}];
}
If I have read this code correct, then a call to -(IBAction)saveEditGolfersView:(id)sender will set all the Golfers with the exact properties, which I expect is not what you want.
I am not quite sure what the problem is, but I hypothesize that you need an NSPredicate to go along with your NSFetchRequest in order to change the correct Golfer(s).
Maybe I missed something, but this code says to me "hey, I'm going to load all of the Golfer in the database, order them by their first name, and then set all of their properties to the exact same text fields on this page". Just sounds like bad news...
To edit just one golfer, be sure to store the golfer you are editing in a property some where. Since you keep the managedObjectContext stored on the applicationDelegate, it will stay alive and thus keep your core data objects alive. That would avoid the expensive fetch that you are doing in the save view. If, however, you do not want to keep a reference to the golfer object, each NSManagedObject has an objectId, which is the identifier used by core data. You could use the objectId in a fetch predicate like so:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"objectId == %#", self.editingGolferObjectId];
[request setPredicate:predicate];
I would choose to keep reference to the object in your case, rather than the objectId

Core data opening file dirty, inconsistently missing values, fetch returns empty array

I have a document based application running with core data. The object model has three entities with several properties. It seems to be working mostly alright—I can fill in some information and save it, no problem. When I go to open the resulting file, however, it always opens "dirty," before I've even touched anything, and a few of the fields are sometimes blank.
What I mean is, sometimes you open the file and those fields show up empty and other times you open the file and they show up with the proper data. The properties that are blank are associated with only one of the entities and are displayed within the same NSTabView. They are some NSStrings displayed as both values in text fields and labels.
Update:
Thanks to #ughoavgfhw's advice, I switched to an XML store and found two problems: that I was creating a new entity each time the document was opened in the [MyDocument init] instead of loading the saved one from the persistent store, but now I'm having problems fetching that one.
In the resulting XML file after a save, it does include this (the entity and properties that are giving me trouble):
<object type="STORY" id="z102">
<attribute name="title" type="string">test 6</attribute>
<attribute name="descript" type="string">this is a test</attribute>
</object>
and I attempt to fetch it with this:
- (Story *)getSavedStory {
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"Story" inManagedObjectContext:[self managedObjectContext]];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSError *error = nil;
NSArray *array = [[self managedObjectContext] executeFetchRequest:request error:&error];
if (array == nil) {
NSLog(#"%#",error);
return nil;
} else {
return [array lastObject];
}
}
After opening that persistent store, that request returns an empty array (and no error). Any tips on where to go from here?
Without code, all I can do is guess, but I would guess that you are doing some setup when you load the document. You do not disable undo registration, which is why you are having it marked as "dirty". There are a few reasons that data could not be loaded correctly. The two most likely situations are that: a) you override the data during your initialization, or b) the data is not being saved correctly, and therefore cannot be loaded correctly.
Here is how to disable undo registration:
NSManagedObjectContext *moc; //In your document subclass, get this with [self managedObjectContext];
[moc processPendingChanges];
[[moc undoManager] disableUndoRegistration];
//Make changes here
[moc processPendingChanges];
[[moc undoManager] enableUndoRegistration];
Update for new information:
Don't make any changes to core data in the init method. The windowControllerDidLoadNib: method is a better choice because everything has been loaded at that point. Here is an example that checks for an existing Story entity and creates a new one if needed:
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController {
[super windowControllerDidLoadNib:windowController];
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:[NSEntityDescription entityForName:#"Story" inManagedObjectContext:[self managedObjectContext]]];
NSError *err = nil;
NSArray *objs = [[self managedObjectContext] executeFetchRequest:req error:&err];
[req release];
if(!objs) {
[[NSAlert alertWithError:err] runModal];
return;
}
NSManagedObject *story = nil;
if([objs count] == 0) {
[[self managedObjectContext] processPendingChanges];
[[self undoManager] disableUndoRegistration];
story = [NSEntityDescription insertNewObjectForEntityForName:#"Story" inManagedObjectContext:[self managedObjectContext]];
//Additional setup
[[self managedObjectContext] processPendingChanges];
[[self undoManager] disableUndoRegistration];
} else story = [objs lastObject];
}

"Save" in CoreData-apps

I've here a CoreData app (no document-based), 1 entity and 1 tableview for edit/add/remove "instances" of the entity. Now I can manual add and save but I would like to
a) save automaticly changes
b) add automaticly some "instances" with the first start.
I think a) can be solved with NSNotifications. But which to use by entities?
Any ideas how to solve a) or b)?
Thanks for every answer. =)
Autosave can be a bit trickier than you'd first expect sometimes, since there may be times when your application data is in an invalid state (for instance, when the user is editing an entity) and either cannot be saved or it would not make sense to save. There's no easy setAutosaves:YES property unfortunately, so you'll have to implement it yourself. Using a notification to save after certain actions is one way to do it, you could also set up a timer to save periodically if it makes sense for your application.
To populate an empty data file, just check to see if the data store is empty at launch (applicationDidFinishLaunching and awakeFromNib are two possible places to put this), and if it is insert some entities as normal. The only tricky part is disabling undo management during the process. Here's an example from one of my applications:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
{
NSURL *fileURL = [NSURL fileURLWithPath:[self.applicationSupportFolder stringByAppendingPathComponent:WLDataFileName]];
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileURL options:nil error:NULL];
[context setPersistentStoreCoordinator:coordinator];
[request setEntity:[NSEntityDescription entityForName:#"Shelf" inManagedObjectContext:context]];
if ( [context countForFetchRequest:request error:NULL] == 0 )
[self _populateEmptyDataStore:context];
_managedObjectContext = [context retain];
[request release];
[coordinator release];
[context release];
// finish loading UI, etc...
}
- (void)_populateEmptyDataStore:(NSManagedObjectContext *)context;
{
[[context undoManager] disableUndoRegistration];
WLSmartShelfEntity *allItems = [NSEntityDescription insertNewObjectForEntityForName:#"SmartShelf" inManagedObjectContext:context];
WLSmartShelfEntity *trash = [NSEntityDescription insertNewObjectForEntityForName:#"SmartShelf" inManagedObjectContext:context];
allItems.name = NSLocalizedString( #"All Items", #"" );
allItems.predicate = [NSPredicate predicateWithFormat:#"isTrash = FALSE"];
allItems.sortOrder = [NSNumber numberWithInteger:0];
allItems.editable = [NSNumber numberWithBool:NO];
trash.name = NSLocalizedString( #"Trash", #"" );
trash.predicate = [NSPredicate predicateWithFormat:#"isTrash = TRUE"];
trash.sortOrder = [NSNumber numberWithInteger:2];
trash.editable = [NSNumber numberWithBool:NO];
[context processPendingChanges];
[[context undoManager] enableUndoRegistration];
DebugLog( #"Filled empty data store with initial values." );
}
Have a look at this thread on the Apple Mailing lists regarding autosave and Core Data.