Monday, March 23, 2015

Onward and Upward

Well when I last left off I was trying to verify step 3 of Apple’s iCloud Programming Guide For Core Data.  I failed miserably.  I am coming to the realization that the document is outdated.  When I removed the app from all my devices and the simulator and waited an hour, and enabled airplane mode the log still said Using Local store 0 and my data was still there.  I have no idea how this is supposed to work or why it isn’t working.

I decided to press onward and maybe something will make sense.  One change I did make is now instead of listening to the Core Data messages for didChange and willChange I switched to use MagicalRecord’s (kMagicalRecordPSCDidCompleteiCloudSetupNotification
and kMagicalRecordDidMergeChangesFromiCloudNotification)

Aggregations:
After getting the top level object to save I turned my attention to how would I manage a child object in an aggregation.  I used a tutorial I got from Ray Wenderlich’s site (http://www.raywenderlich.com).  The general steps are:
  • clone the collection from the parent object into a mutable collection
  • change the collection as desired
  • Set the collection back on the parent object
  • store the parent object

They also alluded to the fact open source projects tend to provide helper methods for this.  I couldn’t find anything looking through the MagicalRecord source so I rolled my own.  

I ran into a problem where during the third step I was having an inconsistent object failure.  It turned out this was how I was loading the parent object to begin with.  I was loading it on a separate thread and then returning it to the main thread.  The end result of all this was the MOC was not set on the parent object and trying to make a change to it later resulted in this error.  This seems like a design flaw in my code.  I need to rethink this.

What about Parse?

I was playing an iOS game last night and they had me sign-up.  I had two choices, enter my email or sign-in via facebook.  It got me to thinking about my experience with Parse.  I might could create my own login/signup page where all I asked for was the user’s email address.  Would that be enough to simulate the iCloud Core Data syncing without having them have to create a full fledged account(and remember their password)?  I need to think more on this one.

I ran across a project called “Sugar Record”  It looks promising, but I’m not willing to bite that one off until it matures more.

Retrieving NSDate from NSManagedObject

I found that in my NSManagedObject (generated by XCode) my date objects were declared as:

@NSManaged var dateEntered: NSDate

This seemed to trip Swift up when I tried to reference the dateEntered attribute (particularly if it was nil).  I ended up having to protect myself from this by using this code:

       if let _dateEntered = object.dateEntered as NSDate? {
           dateEnteredLabel.text = "\(_dateEntered)"
       }

Notice the explicit downcast to an optional NSDate. Kind of strange, but otherwise the compiler complained about the attribute not being able to be nil when clearly it can be.  I wonder if I could manually declare the NSDate attribute on the object as optional?

Here We Go Again

So I was thinking about my progress and I thought that I should try a migration just to make sure I was comfortable with how that would work.  Unfortunately, I started down that path before I knew it.  In making changes, I accidentally changed some attributes of my model without creating a new version.  That caused me to get a “Can’t find model for source store” when testing on my device.  After some investigation I realized that when MagicalRecord sets up the store it sets it up to be auto migrating.  This normally would be fine had I not messed the model up by making a change to it without first creating a new version.  My first attempt at fixing this was to create a new model version and set that as the default.  No luck.  I then poked around a bit and figured I would set MagicalRecord.shouldDeleteStoreOnModelMismatch to true, but that didn’t fix it either, so I was left with what do I do?

I looked around for a way to do a manual migration with MagicalRecord.  I even found a StackOverflow reference back in 2013 to an experimental method to setup the stack so that you could provide a MappingModel. But when I checked the latest code that didn’t seem to have made it in the latest release.  Sooo, I am left with thinking that I need to stand the stack up manually.  I’m not looking forward to that.

Yes I could probably delete the app from my device and reinstall, but what happens with the iCloud store? I still am unclear how the iCloud store would ever get migrated.  Oh well, more to learn I guess.

No comments:

Post a Comment