Using Core Data in iOS with RubyMotion
Learn how to use Core Data in a simple RubyMotion app. By Gavin Morrice.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Using Core Data in iOS with RubyMotion
30 mins
- Getting Started
- Adding the Tasks Button
- Adding the Tasks List Screen
- Populating the Tasks List with Real Data
- Installing CDQ
- Creating a Data Model
- Be the Boss of cdq
- CDQ Helper Methods
- Loading Tasks from the Database
- Creating New Tasks
- Selecting the Current Task
- Editing the Tasks List
- Where To Go From Here?
Editing the Tasks List
You're almost done; there's just one more thing to add, and this feature is complete!
The user should also be able to remove tasks from the list once they're complete. After all, there's nothing more satisfying than cleaning up your TODO list once your work is complete.
Add this by implementing the UITableViewDelegate methods: tableView:canEditRowAtIndexPath:
and tableView:commitEditingStyle:forRowAtIndexPath:
in tasks_view_controller.rb like so:
# 1
def tableView(table_view, canEditRowAtIndexPath: index_path)
todays_tasks.any?
end
# 2
def tableView(table_view, commitEditingStyle:editing_style, forRowAtIndexPath: index_path)
case editing_style
when UITableViewCellEditingStyleDelete
delete_task_at_index(index_path.row)
if todays_tasks.any?
tableView.deleteRowsAtIndexPaths([index_path],
withRowAnimation: UITableViewRowAnimationFade)
else
tableView.reloadRowsAtIndexPaths([index_path],
withRowAnimation: UITableViewRowAnimationFade)
end
end
end
What's going on here?
-
tableView:canEditRowAtIndexPath:
simply returns true if there are Tasks available to tell the controller that the tasks may be edited. - tableView:commitEditingStyle:forRowAtIndexPath is more involved, and I'll talk you through it:
- First, it checks the value of
editing_style
. Here you're only dealing with the valueUITableViewCellEditingStyleDelete
. If that's the case, the task is deleted by callingdelete_task_at_index
(it's not defined yet). - If there are still other tasks in the database, then this row is deleted from the table view with a nice
UITableViewRowAnimationFade
animation, otherwise, the table reloads to show one EmptyCell with aUITableViewRowAnimationFade
animation.
Now define delete_task_at_index
at the bottom of TasksViewController
, just before the closing end
:
def delete_task_at_index(index)
task = todays_tasks[index]
task.destroy
Task.save
Task.reset_current
end
This private method fetches the task to be destroyed by its index in todays_tasks
, marks it for deletion from the database, and then commits the changes to the database. The last line calls the reset_current
method you defined in Task
to clear the current task.
Build and run your app one more time and try adding and removing some tasks
rake device_name="iPhone 4s"
Where To Go From Here?
That's it. You've successfully implemented Core Data with your RubyMotion app. Hopefully you found it refreshingly straightforward.
Of course, there's a lot more you can achieve with Core Data and RubyMotion than what you've covered here. I recommend you check out the fully documented source code, as well as CDQ's README and documentation.
I'd love to hear about how you've implemented Core Data with RubyMotion, anything you discover while you work through this tutorial and any questions that bubble up as you play around around. Leave your notes, comments and questions below!