Search code examples
iosrubyrubymotion

Update NSManagedObject item using RubyMotion


I'm getting up to speed with the wonderful RubyMotion, having trouble updating a NSManagedObject field though, my project is based on the example app Locations

The code in the locations_store.rb has a create and delete ...

def add_location
    # Yield a blank, newly created Location entity, then save the model.
    yield NSEntityDescription.insertNewObjectForEntityForName('Location', inManagedObjectContext:@context)
    save
end

def remove_location(location)
    # Delete the given entity, then save the model.
    @context.deleteObject(location)
    save
end

However I'm struggling to find a way to implement a similar method to update the data store, in my new location_details_controller.rb I am storing an instance of the location like so,

def showDetailsForLocation(location)
    @location = location
    navigationItem.title = "%0.3f, %0.3f" % [location.latitude, location.longitude]
end

then this fires when the save button is hit ...

def saveLocation(sender)

    LocationsStore.shared.update_location(@objectid, "field1updatevalue", "field2updatevalue")
end 

But, I've tried numerious ways to crack this but can't find a way to update the record in this method ...

def update_location(location, f1, f2)
    location.f1 = f1
    location.f2 = f2
    save
end

Solution

  • I bailed out of trying to navigate NSManagedObject myself and used the basic but workable activerecord like wrapper by ClarkWare