Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
Update RubyMotion to use async requests instead of sync
Browse files Browse the repository at this point in the history
  • Loading branch information
dingbat committed Oct 13, 2013
1 parent c32d073 commit 57dcb79
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 47 deletions.
3 changes: 1 addition & 2 deletions demos/rubymotion/app/controllers/input_view_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def save
author = @author_field.text
message = (@content_field.tag == 0 ? "" : @content_field.text) # blank if still on placeholder (tag 0)

# If the block returned true (it worked), we should dismiss
self.cancel if @completion_block.call(author, message)
@completion_block.call(author, message)
end

def viewDidLoad
Expand Down
49 changes: 24 additions & 25 deletions demos/rubymotion/app/controllers/posts_view_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ def add
new_post.author = author
new_post.content = content

ptr = Pointer.new(:object)
if !new_post.remoteCreate(ptr)
AppDelegate.alertForError ptr[0]
# Don't dismiss the input VC
return false
new_post.remoteCreateAsync lambda do |error|
if error
AppDelegate.alertForError error
else
@posts.insert(0, new_post)
self.tableView.reloadData
self.dismissViewControllerAnimated(true, completion:nil)
end
end

@posts.insert(0, new_post)
self.tableView.reloadData

true
end

new_post_vc.header = "Post something to NSRails.com!"
Expand All @@ -31,28 +29,29 @@ def add
end

def refresh
# When the refresh button is hit, refresh our array of posts (uses an extension on Array)
# When the refresh button is hit, refresh our array of posts

e_ptr = Pointer.new(:object)
if @posts.remoteFetchAll(Post, error:e_ptr)
self.tableView.reloadData
else
AppDelegate.alertForError e_ptr[0]
Post.remoteAllAsync lambda do |all_remote, error|
if error
AppDelegate.alertForError error
else
@posts = all_remote
self.tableView.reloadData
end
end

# This could also be done by setting @posts to the result of Post.remoteAll(e_ptr), but using the Array method will persist the same objects and update their respective properties instead of replacing everything, which could be desirable
end

def deletePostAtIndexPath(indexPath)
post = @posts[indexPath.row]

p = Pointer.new(:object)
if post.remoteDestroy(p)
# Remember to delete the object from our local array too
@posts.delete(post)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation:UITableViewRowAnimationAutomatic)
else
AppDelegate.alertForError p[0]
post.remoteDestroyAsync lambda do |error|
if error
AppDelegate.alertForError error
else
# Remember to delete the object from our local array too
@posts.delete(post)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation:UITableViewRowAnimationAutomatic)
end
end
end

Expand Down
39 changes: 19 additions & 20 deletions demos/rubymotion/app/controllers/responses_view_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ def add
new_resp.content = content
new_resp.post = @post # check out response.rb for more detail on how this line is possible

p = Pointer.new(:object)
if !new_resp.remoteCreate(p)
AppDelegate.alertForError p[0]
return false
new_resp.remoteCreateAsync lambda do |error|
if error
AppDelegate.alertForError error
else
@post.responses.insert(0, new_resp)
self.tableView.reloadData
self.dismissViewControllerAnimated(true, completion:nil)
end
end

@post.responses.insert(0, new_resp)
self.tableView.reloadData

# Instead of line 10 (the belongs_to trick), you could also add the new response to the post's "responses" array and then update it:
# Instead of line 16 (the belongs_to trick), you could also add the new response to the post's "responses" array and then update it:
#
# post.responses << newResp
# post.remoteUpdate(error_ptr)
#
# Doing this may be tempting since it'd already be in post's "responses" array, BUT: you'd have to take into account the Response validation failing (you'd then have to *remove it* from the array). Also, creating the Response rather than updating the Post will set newResp's remoteID, so we can do remote operations on it later!

true
end

new_post_vc.header = "Write your response to #{@post.author}"
Expand All @@ -39,18 +38,18 @@ def add
def deleteResponseAtIndexPath(indexPath)
resp = @post.responses[indexPath.row]

ptr = Pointer.new(:object)
if resp.remoteDestroy(ptr)
# Remember to delete the object from our local array too
@post.responses.delete resp
resp.remoteDestroyAsync lambda do |error|
if error
AppDelegate.alertForError error
else
@post.responses.delete resp

self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation:UITableViewRowAnimationAutomatic)

if @post.responses.empty?
self.tableView.reloadSections(NSIndexSet.indexSetWithIndex(0), withRowAnimation:UITableViewRowAnimationAutomatic)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation:UITableViewRowAnimationAutomatic)

if @post.responses.empty?
self.tableView.reloadSections(NSIndexSet.indexSetWithIndex(0), withRowAnimation:UITableViewRowAnimationAutomatic)
end
end
else
AppDelegate.alertForError ptr[0]
end

# If we wanted to batch-delete or something, we could also do:
Expand Down

0 comments on commit 57dcb79

Please sign in to comment.