sxross / MotionModel

Simple Model and Validation Mixins for RubyMotion
MIT License
192 stars 67 forks source link

Alternative adapters? #132

Open cbetta opened 9 years ago

cbetta commented 9 years ago

Is there a list of alternative adapters anywhere? MotionModel::ArrayModelAdapter is a nice starting point but a bit useless beyond initial experiments.

sxross commented 9 years ago

There is the SQL branch, which uses FMDB as a backing store. I've not used it because my needs are more for a backing store with some data manipulation capability instead of a major database search/sort kind of thing. I've also done some strawman integrations with Meteor DDP and with Firebase, but the code is preliminary. What is the use case that makes ArrayModelAdapter insufficient to the task?

cbetta commented 9 years ago

Well I can load my data and then display it, but when the app gets killed in the background I want to have some data ready cached.

sxross commented 9 years ago

I simply set an observer on the MotionModelDataDidChange notification and persist the data store. This is not so expensive that the user ever notices the delay in my experience. Here's a snippet of code I keep in AppDelegate:

  ################ Persistence Is Handled in Asynchronous Queue #################
  def observe_change
    @model_change_observer = App.notification_center.observe MotionModelDataDidChangeNotification do |notification|
      queue = Dispatch::Queue.concurrent('com.calicowebdev.whos_here.task')
      queue.async{
        begin
          save_one(notification.object.class)
        rescue MotionModel::PersistFileError => ex
          App.notification_center.post MotionModelPersistFileError, Time.now,
                                            {:status => 'failure', :exception => ex.message}
        end
        file_sizes
      }
    end
  end

I have a slightly more elegant version that creates a block that observes a given model, but this should give you an idea how I set it up. It will persist changes across settings every time you save/update/delete a collection.