I am tryng to check if there is any data in my coredata storage as a type of recovery for my app. Basicly if the user is in the final view there is some data in coredata that they are constantly updating.
So they are in the final view then the app breaks or they put it to sleep then the app gets removed from memory.
when the app is next loaded I check my coredata object to see if there are any values If there are I prompt the user telling them there is unfinished work would you like to pick up from where you left off of continue fresh.
if they want to start fresh i dump anything thats currently in my core data and allow them to work. else I jump to the last view load up the data thats been in coredata and allow them to continue working.
However this is where the error happens I check my coredata like so.
NSMutableArray *checkFinMutableArray = [coreDataController readFin];
if ([checkFinMutableArray count] > 0) {
//Show mesage, recover or not?
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:#"Selected projects avalible"];
[alert setMessage:#"It appears that you have unfinished projects from a previous session. Would you like to continue working on these projects?"];
[alert setDelegate:self];
[alert addButtonWithTitle:#"Yes"];
[alert addButtonWithTitle:#"No"];
[alert show];
}
this is what my coredata object looks like
- (NSMutableArray *)readFinDimensions {
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Project" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error;
NSMutableArray *projectDictionaryArray = [[NSMutableArray alloc] init];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (ProjectList *projectList in fetchedObjects) {
NSMutableDictionary *tempProjectDictionaryArray = [[ NSMutableDictionary alloc] init];
[tempProjectDictionaryArray setObject:project.proj forKey:#"Proj"]; // this is where the ap dies
[tempProjectDictionaryArray setObject:project.desc forKey:#"Desc"];
[projectDictionaryArray addObject:tempProjectDictionaryArray];
}
return projectDictionaryArray;
}
and this is what the error looks like
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: Proj)'
any help would be greatly appreciated.
May be your code must be:
for (ProjectList *projectList in fetchedObjects) {
NSMutableDictionary *tempProjectDictionaryArray = [[ NSMutableDictionary alloc] init];
[tempProjectDictionaryArray setObject:projectList.proj forKey:#"Proj"]; // this is where the ap dies
[tempProjectDictionaryArray setObject:projectList.desc forKey:#"Desc"];
[projectDictionaryArray addObject:tempProjectDictionaryArray];
}
where project.proj is changed by projectList.proj (also for .desc).
That means that project.proj is nil at
[tempProjectDictionaryArray setObject:project.proj forKey:#"Proj"];
An NS(Mutable)Dictionary cannot store nil as a value, so you should check e.g.
if (project.proj != nil) {
[tempProjectDictionaryArray setObject:project.proj forKey:#"Proj"];
}
Alternatively, you can use
tempProjectDictionaryArray = [project dictionaryWithValuesForKeys:#[#"Proj", #"Desc"]];
which does almost the same as
[tempProjectDictionaryArray setObject:project.proj forKey:#"Proj"];
[tempProjectDictionaryArray setObject:project.desc forKey:#"Desc"];
but replaces nil values by [NSNull null], so you can remove these
from the dictionary with
NSArray *keysForNullValues = [tempProjectDictionaryArray allKeysForObject:[NSNull null]];
[tempProjectDictionaryArray removeObjectsForKeys:keysForNullValues];
Related
I want to insert 200 5Mb records in my Core Database. But when I save the NSManagedObject, the memory wasn't released (autoreleased pool didn't help), and after inserting 30 records I got the memory warning and the application crashed. Here is my code
- (void)SaveItem
{
NSString *entityName = kEntityName;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = appDelegate.managedObjectContext;
NSEntityDescription *entityDesctiption = [NSEntityDescription
entityForName: entityName
inManagedObjectContext:context];
// check if town exists
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"id == %d", self.imageID];
NSFetchRequest *requestToCheckExistense = [[NSFetchRequest alloc] init];
[requestToCheckExistense setEntity:entityDesctiption];
[requestToCheckExistense setPredicate:predicate];
NSArray *objects = [context executeFetchRequest:requestToCheckExistense error:nil];
[requestToCheckExistense release];
if (objects == nil)
{
NSLog(#"there was an error");
}
NSManagedObject *object;
if ([objects count] > 0)
{
// edit item
object = [objects objectAtIndex:0];
}
else
{
// if object doesn't exist, find max id to imlement autoincrement
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesctiption];
request.propertiesToFetch = [NSArray arrayWithObjects: #"id", nil];
NSArray *allobjects = [context executeFetchRequest:request error:nil];
[request release];
NSInteger newID = 1;
if ([allobjects count] > 0)
{
NSNumber *maxID = [allobjects valueForKeyPath:#"#max.id"];
newID = [maxID intValue] + 1;
}
// write item
object = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
[object setValue:[NSNumber numberWithInt:newID] forKey:#"id"];
self.imageID = newID;
}
// fill NSManagedObject
// size of objNSData is about 5MB
NSMutableData *objNSData = [[DatabaseManager sharedDatabaseManager] encryptedDataFromImage:bigImage];
[object setValue:objNSData forKey:#"big"];
[context save:nil];
}
When I commented
[object setValue:objNSData forKey:#"big"];
everything was OK.
I tried to add the code into #autoreleasepool , but that didn't help.
I know, that now, when I save data to database, it's still in iPhone RAM. How to release it from this memory? When I get a set of Managed Objects from the database, they are not in the RAM (I can easyly get 100 object, each of them has 5Mb fields)
object =(tblEntity *) [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
try to type cast the object,this may solve the problem
I've solved the issue.
after call of [self SaveItem];
I used
[context save];
[context reset];
[context save];
all the NSManagedObjects from the context will be released.
After that operation I can add as many big objects as I want
Because you don't own an NSManagedObject when you create it, it may be retained by the core data stack even after releasing it (when using an autoreleasepool contained inside the loop).
This may help:
Set the undo manager of your managedobjectContext to nil:
[context setUndoManager:nil];
Be sure that no properties of that object are retained anywhere, because then the managed object will not be released on time inside your loop.
Be sure to add an autorelease pool inside every loop execution, not wrapping all the loop itself, similar to:
for(i;i<n;i++) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[obj saveItem];
[pool drain];
}
If that object belongs to a hierarchy of NSManagedObjects, then you need to release the owner of this object too, for this one to be deallocated from memory.
You can check apple's documentation about memory management in CoreData.
Warning: big objects (> 1MB) are not recommended by Apple to be stored inside CoreData (Check this other question/answer.)
I am developing an application which requires loading of more than 1 million entries through infinite scrolling in a tableview. Each time request will be sent for 1000 entries and once data is downloaded and parsed through JSON library the table is reloaded. I have implemented this through CoreData with "setFetchBatchSize = 1000".
StreamModal *modal = [[StreamModal alloc]init];
StreamModal *modal = [NSEntityDescription insertNewObjectForEntityForName:#"StreamModal" inManagedObjectContext:managedObjectContext];
if([self isNotNull:[streamDataDict objectForKey:#"_id"]])
modal.stream_id = [streamDataDict objectForKey:#"_id"];
-(void)reloaData{
#try {
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
// Update to handle the error appropriately.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
exit(-1); // Fail
}
NSLog(#"ferchresults count %d",[[_fetchedResultsController fetchedObjects]count]);
}
#catch (NSException *exception) {
NSLog(#"exception raised in reloadData in streamViewController class %#",exception);
}
#finally {
}
}
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"StreamModal" inManagedObjectContext:appDelegate.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:#"date" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:1000];
//[fetchRequest setFetchLimit:2000];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
fetchRequest = nil;
theFetchedResultsController = nil;
return _fetchedResultsController;
}
Here is the code i am using, when ever connectionfinishedloading data i am populating the data into NSManagedObject Class(StreamModal) and then calling reload data.Here the problem is the app is getting memory exceptions after i loaded 12000 entries in table and getting crashed. how can i load all entries without memory exception. i am new to CoreData concepts and have read the core data concepts through developer guide, but i didn't find any info related to memory handling. Please help me.
I hope you are using ARC? Because you're not releasing any initalized objects. (If not this is your answer.)
But anyway: Have you tried to use Instruments to see, which objects are increasing the memory footprint at most? That would be a good starting point.
shiva inturi,
First, I want to echo other comments that working with a single table view of a million items is really a bad user experience.
To your question:
What are you doing when the memory warning comes?
At minimum, you should be going through the objects array and trim the object graph. This is done with -refreshObject:mergeChanges:. You should also take care to not traverse your array very far. I would start from your visible objects and work both backwards and forwards until you start hitting faulted objects, by testing with -isFault.
Andrew
I'm not sure how to use the XMPPFramework's core data to store incoming messages. Does anyone have any tutorials on how to do this? I see User objects, which in turn can have many "resources". Is each message received supposed to be a new resource that I create and persist?
I do not know what part is my responsibility and what part the framework provides regarding message history. I can intercept every incoming message. Then am I supposed to create and store each message inside a Messages table using core data?
I'd have a Message Entity. And each XMPPUser would have an array of Message objects. But then wouldn't I be rolling my own solution, which would be working against the framework?
Thanks!
I know this is an old thread but as I am currently working with XMPP on iOS I must say that there is a built in support for archiving messages in XMPP.
I downloaded the XMPP framework for iOS and in it there is folder marked XEP-0136. Import the folders in XCode and activate client side archiving by using the following lines of code in the class you instantiate XMPP client:
xmppMessageArchivingStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
xmppMessageArchivingModule = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:xmppMessageArchivingStorage];
the following one line of code saves you from sending archive specific stanzas to the xmpp server
which will most probably respond with service-not-implemented
[xmppMessageArchivingModule setClientSideMessageArchivingOnly:YES];
[xmppMessageArchivingModule activate:xmppStream];
[xmppMessageArchivingModule addDelegate:self delegateQueue:dispatch_get_main_queue()];
And you are set. From that moment on, messages (outgoing and incoming) will be stored in a table created by the framework.
If you need more info please comment and i will get back to you.
#PraviJay
I did like this :
-(void)testMessageArchiving{
XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"XMPPMessageArchiving_Message_CoreDataObject"
inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entityDescription];
NSError *error;
NSArray *messages = [moc executeFetchRequest:request error:&error];
[self print:[[NSMutableArray alloc]initWithArray:messages]];
}
-(void)print:(NSMutableArray*)messages{
#autoreleasepool {
for (XMPPMessageArchiving_Message_CoreDataObject *message in messages) {
NSLog(#"messageStr param is %#",message.messageStr);
NSXMLElement *element = [[NSXMLElement alloc] initWithXMLString:message.messageStr error:nil];
NSLog(#"to param is %#",[element attributeStringValueForName:#"to"]);
NSLog(#"NSCore object id param is %#",message.objectID);
NSLog(#"bareJid param is %#",message.bareJid);
NSLog(#"bareJidStr param is %#",message.bareJidStr);
NSLog(#"body param is %#",message.body);
NSLog(#"timestamp param is %#",message.timestamp);
NSLog(#"outgoing param is %d",[message.outgoing intValue]);
}
}
}
Hope it helps :)
The responses that indicate XMPP Framework doesn't save the history are incorrect.
To integrate results in a table view use:
XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"XMPPMessageArchiving_Contact_CoreDataObject"
inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entityDescription];
_contactsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:moc sectionNameKeyPath:nil cacheName:#"MessagesContactListCache"];
NSError *error;
BOOL rval = [_contactsController performFetch:&error];
an example to get archived messages in Swift 4
declares and initializes the variables XMPPMessageArchivingCoreDataStorage where I initialize the XMPPStream
var xmppMessageStorage: XMPPMessageArchivingCoreDataStorage?
var xmppMessageArchiving: XMPPMessageArchiving?
xmppMessageStorage = XMPPMessageArchivingCoreDataStorage.sharedInstance()
xmppMessageArchiving = XMPPMessageArchiving(messageArchivingStorage: xmppMessageStorage)
xmppMessageArchiving?.clientSideMessageArchivingOnly = true
xmppMessageArchiving?.activate(stream)
xmppMessageArchiving?.addDelegate(self, delegateQueue: DispatchQueue.main)
doing this, whenever a message arrives, this will cause it to be archived without needing to do anything else.
then, to retrieve the archived message
func RecibedMessageArchiving(idFriend: String) {
let JabberIDFriend = idFriend //id friend chat, example test1#example.com
let moc = xmppMessageStorage?.mainThreadManagedObjectContext
let entityDescription = NSEntityDescription.entity(forEntityName: "XMPPMessageArchiving_Message_CoreDataObject", in: moc!)
let request = NSFetchRequest<NSFetchRequestResult>()
let predicateFormat = "bareJidStr like %# "
let predicate = NSPredicate(format: predicateFormat, JabberIDFriend)
request.predicate = predicate
request.entity = entityDescription
//jabberID id del usuario, cliente
var jabberIDCliente = ""
if let jabberj = globalChat.value(forKey: "jabberID"){
jabberIDCliente = jabberj as! String
}
do {
let results = try moc?.fetch(request)
for message: XMPPMessageArchiving_Message_CoreDataObject? in results as? [XMPPMessageArchiving_Message_CoreDataObject?] ?? [] {
var element: DDXMLElement!
do {
element = try DDXMLElement(xmlString: (message as AnyObject).messageStr)
} catch _ {
element = nil
}
let body: String
let sender: String
let date: NSDate
let isIncomings: Bool
if message?.body != nil {
body = (message?.body)!
} else {
body = ""
}
if element.attributeStringValue(forName: "to") == JabberIDFriend {
sender = jabberIDCliente
isIncomings = false
} else {
sender = "test2#example.com"
isIncomings = true
}
var m: [AnyHashable : Any] = [:]
m["msg"] = message?.body
print("body", message?.body)
print("test", element.attributeStringValue(forName: "to"))
print("test2", element.attributeStringValue(forName: "body"))
}
} catch _ {
//catch fetch error here
}
}
XMPPFramework does not store message history,So i suggest to you it is better to use core data.Create a table by taking sender,receiver,message,time as columns .Insert record when send message method calling and receive message method calling...
-(void)saveChatHistory:(NSString *)sender:(NSString*)receiver:(NSString*)message:(NSString*)time
{
NSManagedObjectContext *context=[[self appDelegate] managedObjectContext];
NSManagedObject *newContext=[NSEntityDescription insertNewObjectForEntityForName:#"ChatHistory" inManagedObjectContext:context];
[newContext setValue:sender forKey:#"sender"];
[newContext setValue:receiver forKey:#"receiver"];
[newContext setValue:message forKey:#"message"];
[newContext setValue:time forKey:#"time"];
NSError *error;
if(![context save:&error])
{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Error Occured" message:#"Data is not Stored in Database Try Again" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alertView show];
}
}
Retrive chat history when specific user selected from tableview.... the fallowing method shows how to retrive chat history...and call this method from didSelectRowAtIndexPath method and pass destination id as parameter
-(void)getChatHistory:(NSString*)jidString1
{
NSManagedObjectContext *context=[[self appDelegate] managedObjectContext];
NSEntityDescription *entity=[NSEntityDescription entityForName:#"ChatHistory" inManagedObjectContext:context];
NSFetchRequest *req=[[NSFetchRequest alloc] init];
NSPredicate *predicate=[NSPredicate predicateWithFormat:#"receiver=%#",jidString1];
[req setEntity:entity];
[req setPredicate:predicate];
NSManagedObject *matchRecords=nil;
NSError *error;
NSArray *objects=[context executeFetchRequest:req error:&error];
if([objects count]==0)
{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"No Record found" message:#"there is no previous chat history" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alertView show];
}
else
{
for(int i=0;i<[objects count];i++)
{
matchRecords=[objects objectAtIndex:i ];
NSLog(#"sender is %#",[matchRecords valueForKey:#"sender"]);
NSLog(#"reciver is %#",[matchRecords valueForKey:#"receiver"]);
NSLog(#"messages is %#",[matchRecords valueForKey:#"message"]);
NSLog(#"time is %#",[matchRecords valueForKey:#"time"]);
}
}
}
I hope this is useful to you
I desperately need help with a memory leak in my iPhone app. The app is ready to submit to the app store, is stable, has no memory leaks at all in iPhone simulator or Clang ... but seems riddled with them on my iPod Touch.
They all seem to stem from managedObjectModel when I'm trying to retrieve data from Core Data.
The Core Data code in my app was automatically created by Xcode a while back, I've noticed that the code has since changed when you get xcode to generate it ... I've tried with the old and new but it makes no difference.
If I comment out the following code, the problem goes away ... can anyway see what's wrong with it? I've spent 9 hours on this so far and just can't figure it out!
NSString *entityForName = [[NSString alloc] initWithString:#"OfflineSettings"];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityForName inManagedObjectContext:[self managedObjectContext]];
[request setEntity:entity];
[entityForName release];
NSSortDescriptor *sortById = [[NSSortDescriptor alloc] initWithKey:#"Id" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortById]];
[sortById release];
NSError *error;
NSMutableArray *mutableFetchResults = [[[self managedObjectContext] executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
NSLog(#"Error fetching");
}
int intId = -1;
if ([mutableFetchResults count] == 0) {
TTDERROR(#"No id has been saved to offline settings");
} else {
OfflineSettings *offlineSettings = (OfflineSettings *)[mutableFetchResults objectAtIndex:0];
intId = [offlineSettings.Id intValue];
}
[mutableFetchResults release];
[request release];
The leak specifically seems to be on this line:
NSMutableArray *mutableFetchResults = [[[self managedObjectContext] executeFetchRequest:request error:&error] mutableCopy];
.. and the code for [self managedObjectContext] is as follows in case it helps ..
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext_ != nil) {
return managedObjectContext_;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext_ = [[NSManagedObjectContext alloc] init];
[managedObjectContext_ setPersistentStoreCoordinator:coordinator];
}
return managedObjectContext_;
}
I'm really at a loss, so I would be so grateful for some help!
Steven
You don't need the mutable copy. executeFetchRequest: returns an autoreleased static array and you're not mutating the array. (I keep seeing this. Must be in an example somewhere.) Likewise, creating the entityForName NSString is pointless. Just put the string literal in the entityForName: to eliminate another possible source of error.
Niether of these are the likely source of the leak but you should remove them anyway.
As a rule of thumb, if you have troubles on device but not simulator or on one hardware but not others, then the problem is in a library/framework that is not properly compiled for the hardware where the error occurs. There really isn't any type of coder error that leaks in one environment but not others. When we make a mistake, it's universal.
It's also possible for resources such as images and sounds to behave differently because different devices use different graphics and audio hardware. That, however, is rather rare.
If you run the code through Instruments it should tell you exactly what object is leaking.
I'm stuck trying to figure out how to modify the data contained on the persistent store.
I'm writing an application with several views using a UITabBarController, my core data methods are located mainly on the main application delegate but I will only be using this data from the UItableViewController view.
In order to use the managedObjectContext created in the main application delegate from the UITableViewController I use the following on the viewDidLoad: method:
MessageAppDelegate *appDelegate = (MessageAppDelegate *)[[UIApplication sharedApplication] delegate];
managedObjectContext = [appDelegate managedObjectContext];
The application then displays some messages in the table and when a user selects a UITableViewCell (didSelectRowAtIndexPath) I get the ID of the message object and call the following method:
[self readMessage:pk];
-(void)readMessage:(NSInteger)pk {
// First I select the data
NSFetchRequest *request = [[NSFetchRequest alloc] init];
// had to setReturnsObjectsAsFaults to NO so I could access the message data
[request setReturnsObjectsAsFaults:NO];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Message" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"pk == %d", pk];
[request setPredicate:predicate];
NSError *error;
NSArray *items = [self.managedObjectContext executeFetchRequest:request error:&error];
[request release];
// Then I update the object
for (Message *thisMessage in items) {
//I display the message to the console before updating to check the value
DLog(#"before reading message %#", thisMessage);
// we set the message flat to YES
[thisMessage setRead:YES];
// we set some sample text here (just for testing)
[thisMessage setMessageText:#"New message text"];
// I then display the message to the console checking that the flag and text has been updated
DLog(#"read message %#", thisMessage);
}
// Finally I save the updated message calling the function posted below
[self saveMOC];
}
- (void)saveMOC {
NSError *error;
if (![managedObjectContext save:&error]) {
NSLog(#"there was an error saving the message!");
}
}
After that the data gets updated correctly and if I fetch the data from the managedObjectContext after saving it I get the correct values.
I verified this by adding the following code to at the end of readMessage method:
request = [[NSFetchRequest alloc] init];
//required to avoid presenting objects as faults!!
[request setReturnsObjectsAsFaults:NO];
entity = [NSEntityDescription entityForName:#"Message" inManagedObjectContext:[self managedObjectContext]];
[request setEntity:entity];
//Set the sort descriptor
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"pk" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
//Execute the request
NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error later
DLog(#"ERROR: Unable to fetch the results");
}
[self setMessagesArray:mutableFetchResults];
NSLog(#"Data now is: %#", mutableFetchResults);
[mutableFetchResults release];
[request release];
The problem is that if I exit from the application and launch it again all my messages lose the read property (or any other changes I make) and the tableview loads the data as it was first saved onto the persistent store.
Try this and see if the object changes are actually being saved
- (void)saveMOC {
NSError *error;
if (![managedObjectContext save:&error]) {
NSLog(#"there was an error saving the message!");
} else {
NSLog(#"The message was saved!");
}
}
So for each call of saveMOC which is successful, you should see a console message. If it is being called and you're seeing the messages, then you must not be altering the 'read message' property. You could check this by inspecting the value of the 'read message' property before and after setting it either using a breakpoint or by using NSLog messages to print its value
Is -readMessage: method defined in your app delegate or in your view controller? My guess is that you're changing properties of an object in different managed object context than one where you try to save changes (MOC in your app delegate), which actually doesn't have an idea that something has changed. On the other hand, MOC which keeps your changes is never saved (changes are kept only in memory) and that for your changes are lost after you restart your app.
Can this be the situation?