UA-17470720-3

Jump to content


Photo
- - - - -

what's needed to use both STV+CoreData and MagicalRecord?

models data-definitions data-stores

  • Please log in to reply
8 replies to this topic

#1 markr

markr

    Sr. Member

  • STV 5.0 Pro
  • PipPipPip
  • 41 posts
Reputation: 5
Good

Posted 12 February 2014 - 02:48 PM

Hello -

 

I have STV+CoreData working - love it!!!

 

I'm also attempting to use another framework called Magical Record (https://github.com/m...a/MagicalRecord) that's required by yet another framework for it to perform CRUD on CoreData objects behind the scenes of my STV powered UI.  I'm able to build the project successfully (i.e. no compilation errors); but, MagicalRecord throws errors about merging models with different entities of the same name as soon as it tries to initialize.  So far in my debugging it looks like this has to do with using Core Data with the same Data Model definition as is used by STV.

 

I'm working on this - thought I'd go ahead and post to see if there's anyone out there who's tried something similar...  Any thoughts on making these two frameworks co-exist peacefully using the same Core Data stack would be appreciated.

 

Thanks,

Mark



#2 Dave Guerin

Dave Guerin

    Forum Master

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 539 posts
  • LocationIreland
Reputation: 134
Popular

Posted 13 February 2014 - 01:35 AM

Hi markr,

 

I don't think it's specifically a MagicalRecord problem, that's just a wrapper around Core Data. 

 

It's likely that you have two versions of your datamodel, either because you have renamed one and the old one is still hanging around in DerivedData and/or on the device/simulator, or because there's an issue with a Core Data migration you've set up.

 

Deleting all your DerivedData and deleting the app from the device/simulator and starting afresh should clear the issue.

 

More information here:

 

http://stackoverflow...ities-named-foo


Cheers,

Dave

www.dgapps.ie

#3 markr

markr

    Sr. Member

  • STV 5.0 Pro
  • PipPipPip
  • 41 posts
Reputation: 5
Good

Posted 13 February 2014 - 10:32 PM

Hi Dave - Thank you.  I believe you are correct.  The problem is/was something to do with data model versions.  I've worked around this by going the other direction to add frameworks to a project - I started a new  project using a demo for the other library and added STV+CoreData to it.  That's working - sort of...  I'm still not totally sure what happened to the data model in the xCode project I was using.

 

Hi All -

 

The "sort of" is - now that things are partially working, when I trigger the other frameworks using a button in the simulator, sometimes they find data saved by STV and sometimes they don't.  The other frameworks are used to push the data off the device (in this case, the simulator) to a service.  Sometimes the data arrives; sometimes it doesn't.  And, sometimes the app gets frozen in an odd state.  I've seen several "SIGKILL" messages.  I believe this has to do with Core Data on the simulator not actually saving to the underlying datastore (SQLite database) until the app is exited by pressing the home button.  Next up is to try adding a save:nil on the managedObjectContext as defined by STV in the code that triggers the other frameworks.  I believe that will force all add/edit/delete actions to complete in the database so the content will be there when MagicalRecord tries to access it.

 

Is there a property on SCTableViewController that can be set to cause "Done" to commit the save in the database immediately (i.e. without pressing the home button)?

 

Also - is this behavior different when run on a device instead of in the simulator?

 

Thanks again to all for the help - it's much appreciated.

 

--- Mark



#4 Dave Guerin

Dave Guerin

    Forum Master

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 539 posts
  • LocationIreland
Reputation: 134
Popular

Posted 14 February 2014 - 02:58 AM

Hi Mark,

Hi Dave - Thank you. I believe you are correct. The problem is/was something to do with data model versions. I've worked around this by going the other direction to add frameworks to a project - I started a new project using a demo for the other library and added STV+CoreData to it. That's working




Great!


I believe this has to do with Core Data on the simulator not actually saving to the underlying datastore (SQLite database) until the app is exited by pressing the home button.



That's correct, as noted in the eBook on page 27.

Next up is to try adding a save:nil



save:&error and do something if there is an error!

on the managedObjectContext as defined by STV in the code that triggers the other frameworks. I believe that will force all add/edit/delete actions to complete in the database so the content will be there when MagicalRecord tries to access it.




Correct. But don't think of Core Data as a database, it's not. Core Data is "an object graph and persistence framework". If you go anywhere near the SQLite you are doing it wrong IMHO.


This Mac application, CoreDataPro, helps a LOT when looking at the Core Data store in the simulator while you are running your app in the simulator:


https://github.com/y...CoreDataUtility


Is there a property on SCTableViewController that can be set to cause "Done" to commit the save in the database immediately (i.e. without pressing the home button)?




I use the following to call a method to save Core Data:

sectionActions.detailModelWillDismiss



-(void)tableViewModelDidEndEditing:(SCTableViewModel *)tableViewModel




If the method you call to do the save (and check for errors and handle any errors) also first asks Core Data if it hasChanges, and only do the save:&error if there are changes, then there isn't much overhead.


Also - is this behavior different when run on a device instead of in the simulator?




It's the same on a device and in the simulator.

Edited by Dave Guerin, 14 February 2014 - 02:59 AM.

  • markr likes this
Cheers,

Dave

www.dgapps.ie

#5 markr

markr

    Sr. Member

  • STV 5.0 Pro
  • PipPipPip
  • 41 posts
Reputation: 5
Good

Posted 14 February 2014 - 09:38 AM

Hi Dave -

 

CoreDataPro is excellent  - many thanks for the link!

 

I added the code snippet below  (I did see your comment re: replacing save:nil with save:&error...one small step at a time...). 

 

And - it's now mostly working - the additional framework is pushing the content to the server without having to click the home button or leave the simulator session. 

 

Next up - figuring out why it's getting duplicates on the server...I can see in CoreDataPro that there's only one set locally...

 

Then on to adding try/catch for the potential &error in the save.

 

Thanks again,

Mark

 

    objectsSection.sectionActions.detailModelWillDismiss = ^(SCTableViewSection *section, SCTableViewModel *detailModel, NSIndexPath *indexPath)
    {
        DCLog(@"ToDo Detail Saving context");
        NSManagedObjectContext *managedObjectContext = [(FTAAppDelegate *)[UIApplication sharedApplication].delegate managedObjectContext];
        [managedObjectContext save:nil];
        DCLog(@"ToDo Detail Context Saved");
    };
    
    [self.tableViewModel addSection:objectsSection];
}

- (void)tableViewModelDidEndEditing:(SCTableViewModel *)tableModel {
    DCLog(@"ToDo Detail Saving context");
    NSManagedObjectContext *managedObjectContext = [(FTAAppDelegate *)[UIApplication sharedApplication].delegate managedObjectContext];
    [managedObjectContext save:nil];
    DCLog(@"ToDo Detail Context Saved");
}

 

and in the .h in the @interface:

 

- (void)tableViewModelDidEndEditing;



#6 Dave Guerin

Dave Guerin

    Forum Master

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 539 posts
  • LocationIreland
Reputation: 134
Popular

Posted 14 February 2014 - 10:28 AM

Hi Mark,

 

You don't need - (void)tableViewModelDidEndEditing; in your .h you just need to make the class a SCTableViewModelDelegate.

 

And I think MagicalRecord has a fully error trapping save: method that might give you some clues. 

 

You should at the very least NSLog any error.

 
I can't really help much with duplicates on the server, it's going to be somewhere in your framework thats performing the CRUD.

Cheers,

Dave

www.dgapps.ie

#7 Laeger

Laeger

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 59 posts
Reputation: 6
Good

Posted 12 March 2014 - 02:08 PM

I'm guessing that the other framework you are referring to is FTASync, and if so the duplicate issue is almost certainly due to that framework.  I responded to your other post, but FTASync has many outstanding sync issues that were not resolved and is now abandoned by the author.

 

EDIT: I have MR and STV working with no issue.  The thing to be careful of is making sure STV is using the same context throughout.


Edited by Laeger, 12 March 2014 - 02:09 PM.


#8 markr

markr

    Sr. Member

  • STV 5.0 Pro
  • PipPipPip
  • 41 posts
Reputation: 5
Good

Posted 15 March 2014 - 11:13 PM

Yep - the other framework is (at this point it's now "was") FTASync.  I've dropped both MagicalRecord and FTASync and am running with custom code to sync and retrieve as outlined in the other post (http://sensiblecocoa...rking-with-stv/).  I'm going to have to go back and look at MagicalRecord again for the import as you suggested there...

 

--- Mark



#9 Laeger

Laeger

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 59 posts
Reputation: 6
Good

Posted 16 March 2014 - 03:59 AM

Yeah I HIGHLY recommend MagicalRecord, and MOGenerator also if you are not already using it.  Grab the develop branch, not master.  Also, try not to use -MR_contextForCurrentThread.  The MR team does not recommend that method and it will be removed in MR 3.0.







Also tagged with one or more of these keywords: models, data-definitions, data-stores

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users