Using Core Data in iOS with RubyMotion

Learn how to use Core Data in a simple RubyMotion app. By Gavin Morrice.

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

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?

  1. tableView:canEditRowAtIndexPath: simply returns true if there are Tasks available to tell the controller that the tasks may be edited.
  2. 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 value UITableViewCellEditingStyleDelete. If that's the case, the task is deleted by calling delete_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 a UITableViewRowAnimationFade 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"

Tasks screen with deleted task

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!