Rubymotion multilevel BW::HTTP calls return early -
i attempting call method, from_server, giving block accept results. from_server loads list of items api each in list calls function add_update giving block accept results each detail. when of details have been handled, from_server calls block return summary.
i can low levels work ok. problem first function calls block before done.
not sure why or need put block.call
the main caller
from_server |updates, inserts| puts "\n updates = #{updates}" puts " inserts = #{inserts}\n\n" puts 'all done' end
the from_server function calles
def from_server(&block) inserts = 0 updates = 0 channels = [1,2,4,6,7] channels.each add_update("/api/channels/#{channel}", &block) |added, channel| if added inserts += 1 else updates += 1 end #do things channel end block.call(updates, inserts) unless block.nil? # problem here gets returned channels have been started on thread end end
gets list , processes each item
def from_server(&block) uri = '/api/channels' apirequest.get(uri) |header, body| new_count = 0 update_count = 0 puts "\nlist body = #{body}\n" body.each |channel| add_update(channel['uri']) |new, ad_channel| if new puts "\nnew #{ad_channel}" new_count += 1 else puts "\nupdate #{ad_channel}" update_count += 1 end puts end end puts "\ndone\n" block.call(update_count, new_count) unless block.nil? puts "all requests done\n" end puts "request channels started" end
processes each item
def add_update(channel_uri, &block) apirequest.get(channel_uri) |header, body| if ad_channel = advchannel.find(name: body['name']).first puts "\n#{ad_channel.name} exists" new = false else ad_channel = advchannel.create(name: body['name'], phone: body['phone'], url: body['uri'], admin_url: body['admin_url'], channel_code: body['channel_code'], uri: channel_uri) puts "\ninserting #{ad_channel.name} - #{ad_channel.id}" new = true end block.call(new, ad_channel) if block_given? end
the results executing caller are: give me following:
request channels started (expected) list body string (expected) add update 1 started (expected) done (unexpected should after done) updates = 0 (unexpected should after done) inserts = 0 (unexpected should after done) done (unexpected should after done) requested done (unexpected should after done) insert or update information each channel (expected)
how summary after complete?
Comments
Post a Comment