lib/motion/util.rb in rm-extensions-0.3.1 vs lib/motion/util.rb in rm-extensions-0.3.2

- old
+ new

@@ -6,9 +6,51 @@ def self.debug!(bool=true) @debug = bool end + # LongTask encapsulates beginBackgroundTaskWithExpirationHandler/endBackgroundTask: + # + # RMExtensions::BackgroundTask.new("my long task") do |task| + # do_something_long + # task.end! + # end + # + # RMExtensions::BackgroundTask.new("my long task") do |task| + # do_something_long_async do + # # later this long task finishes... + # task.end! + # end + # end + # + class LongTask + attr_accessor :bgTask, :desc + + # RMExtensions::BackgroundTask.new("my long task") { |task| task.end! } + def initialize(desc=nil, &block) + @desc = desc + @bgTask = UIApplication.sharedApplication.beginBackgroundTaskWithExpirationHandler(lambda do + if ::RMExtensions.debug? + p "ERROR: #{self.inspect} #{@desc} didn't call #end! in time!" + end + UIApplication.sharedApplication.endBackgroundTask(@bgTask) + end.weak!) + block.call(self) + self + end + + def end! + if ::RMExtensions.debug? + p "SUCCESS: #{self.inspect} #{@desc} ended successfully." + end + if @bgTask != UIBackgroundTaskInvalid + UIApplication.sharedApplication.endBackgroundTask(@bgTask) + @bgTask = UIBackgroundTaskInvalid + end + end + + end + module ObjectExtensions module Util # Raises an exception when called from a thread other than the main thread.