I've been experimenting with RubyMotion and MotionModel. Been through the entire MotionModel tutorial (https://github.com/sxross/MotionModel/wiki/Tutorial) but it doesn't cover serialization and persistent storage of model data.
I was able to get data serialized and stored and can (re)load form data using notifications, but can't seem to load up the model data properly as the app is started. I get a blank tableView, then when I create a new instance of the model all of the data pops in at once. I know it's something simple but I just am not seeing it.
AppDelegate:
class AppDelegate
attr_reader :window
def application(application, didFinishLaunchingWithOptions:launchOptions)
controller = EntryController.alloc.init
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@window.rootViewController = UINavigationController.alloc.initWithRootViewController(controller)
@window.rootViewController.wantsFullScreenLayout = true
@window.makeKeyAndVisible
Entry.deserialize_from_file('entries.dat')
true
end
end
Controller:
MotionModelDataDidChangeNotification = 'MotionModelDataDidChangeNotification'
class EntryController < UIViewController
def viewDidLoad
super
self.title = "Entries"
@entry_model_change_observer = App.notification_center.observe MotionModelDataDidChangeNotification do |notification|
if notification.object.is_a?(Entry)
reload_data
end
end
#create and add tableview
@table = UITableView.alloc.initWithFrame([[0,0],[320,480]])
@table.dataSource = @table.delegate = self
self.view.addSubview @table
##adds top right "new" button
right_button = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemAdd, target:self, action:"new_entry")
self.navigationItem.rightBarButtonItem = right_button
end
def viewWillDisappear(animated)
App.notification_center.unobserve @task_model_change_observer
end
def reload_data
@entries = Entry.all
@table.reloadData
end
def new_entry
Entry.create :details => "New entry"
end
def numberOfSectionsInTableView(view)
1
end
def tableView(tableView, numberOfRowsInSection: section)
Entry.count
end
def tableView(tableView, cellForRowAtIndexPath: indexPath)
@reuseIdentifier ||= "entry_cell"
cell = tableView.dequeueReusableCellWithIdentifier(@reuseIdentifier) || begin
UITableViewCell.alloc.initWithStyle UITableViewCellStyleSubtitle, reuseIdentifier:@reuseIdentifier
end
entry = @entries[indexPath.row]
cell.textLabel.text = entry.details
cell.detailTextLabel.text = entry.details
cell
end
def tableView(tableView, didSelectRowAtIndexPath:indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
end
end
I've tried calling reload_data
and embedded various renditions of that method's code inside viewDidLoad
but not getting anywhere with it. Any guidance is much appreciated!