Class: RMXLongTask
- Inherits:
-
Object
- Object
- RMXLongTask
- Includes:
- RMXCommonMethods
- Defined in:
- lib/motion/RMXLongTask.rb
Overview
RMXLongTask encapsulates beginBackgroundTaskWithExpirationHandler/endBackgroundTask:
RMXLongTask.new(“my long task”) do |task|
do_something_long
task.end!
end
RMXLongTask.new(“my long task”) do |task|
do_something_long_async do
# later this long task finishes...
task.end!
end
end
Instance Attribute Summary collapse
-
#bgTask ⇒ Object
Returns the value of attribute bgTask.
-
#desc ⇒ Object
Returns the value of attribute desc.
Class Method Summary collapse
- ._creator(desc = nil, verbose = false, tracking = true, &block) ⇒ Object
-
.create(desc = nil, &block) ⇒ Object
RMXLongTask.create(“my long task”) { |task| task.end! }.
- .internal(desc = nil, &block) ⇒ Object
- .outstanding_queue ⇒ Object
- .outstanding_tasks ⇒ Object
- .reset_outstanding_tasks! ⇒ Object
- .time_remaining ⇒ Object
-
.verbose(desc = nil, &block) ⇒ Object
RMXLongTask.verbose(“my long task”) { |task| task.end! }.
- .when_all_complete(&block) ⇒ Object
Instance Method Summary collapse
- #__end! ⇒ Object
- #end! ⇒ Object
-
#initialize(desc = nil, verbose = false, tracking = true) ⇒ RMXLongTask
constructor
A new instance of RMXLongTask.
- #rmx_dealloc ⇒ Object
Methods included from RMXCommonMethods
#dealloc, #description, #inspect
Constructor Details
#initialize(desc = nil, verbose = false, tracking = true) ⇒ RMXLongTask
Returns a new instance of RMXLongTask.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/motion/RMXLongTask.rb', line 80 def initialize(desc=nil, verbose=false, tracking=true) @verbose = verbose @tracking = tracking @desc = "#{rmx_object_desc} #{desc}" @bgTask = UIApplication.sharedApplication.beginBackgroundTaskWithExpirationHandler(lambda do p "ERROR: #{@desc} didn't call #end! in time!" __end! end) if DEBUG_LONGTASK || @verbose p "CREATED: #{@desc}" end if @tracking RMXLongTask.outstanding_queue.sync do RMXLongTask.outstanding_tasks << self end end self end |
Instance Attribute Details
#bgTask ⇒ Object
Returns the value of attribute bgTask.
16 17 18 |
# File 'lib/motion/RMXLongTask.rb', line 16 def bgTask @bgTask end |
#desc ⇒ Object
Returns the value of attribute desc.
16 17 18 |
# File 'lib/motion/RMXLongTask.rb', line 16 def desc @desc end |
Class Method Details
._creator(desc = nil, verbose = false, tracking = true, &block) ⇒ Object
64 65 66 67 68 |
# File 'lib/motion/RMXLongTask.rb', line 64 def self._creator(desc=nil, verbose=false, tracking=true, &block) x = new(desc, verbose, tracking) block.weak!.call(x) x end |
.create(desc = nil, &block) ⇒ Object
RMXLongTask.create(“my long task”) { |task| task.end! }
56 57 58 |
# File 'lib/motion/RMXLongTask.rb', line 56 def self.create(desc=nil, &block) _creator(desc, false, true, &block) end |
.internal(desc = nil, &block) ⇒ Object
60 61 62 |
# File 'lib/motion/RMXLongTask.rb', line 60 def self.internal(desc=nil, &block) _creator(desc, false, false, &block) end |
.outstanding_queue ⇒ Object
36 37 38 39 40 41 |
# File 'lib/motion/RMXLongTask.rb', line 36 def self.outstanding_queue Dispatch.once do @outstanding_queue = Dispatch::Queue.new("#{NSBundle.mainBundle.bundleIdentifier}.outstanding.LongTask") end @outstanding_queue end |
.outstanding_tasks ⇒ Object
43 44 45 46 47 48 |
# File 'lib/motion/RMXLongTask.rb', line 43 def self.outstanding_tasks Dispatch.once do @outstanding_tasks = [] end @outstanding_tasks end |
.reset_outstanding_tasks! ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/motion/RMXLongTask.rb', line 70 def self.reset_outstanding_tasks! RMXLongTask.outstanding_queue.sync do size = RMXLongTask.outstanding_tasks.size if size > 0 p "WARNING: reset_outstanding_tasks! (was: #{size})" RMXLongTask.outstanding_tasks.removeAllObjects end end end |
.time_remaining ⇒ Object
20 21 22 |
# File 'lib/motion/RMXLongTask.rb', line 20 def self.time_remaining UIApplication.sharedApplication.backgroundTimeRemaining end |
.verbose(desc = nil, &block) ⇒ Object
RMXLongTask.verbose(“my long task”) { |task| task.end! }
51 52 53 |
# File 'lib/motion/RMXLongTask.rb', line 51 def self.verbose(desc=nil, &block) _creator(desc, true, true, &block) end |
.when_all_complete(&block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/motion/RMXLongTask.rb', line 24 def self.when_all_complete(&block) Dispatch::Queue.main.async do if RMXLongTask.outstanding_tasks.size.zero? RMX.block_on_main_q(block) else RMX.new(RMXLongTask).once(:all_complete) do block.call end end end end |
Instance Method Details
#__end! ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/motion/RMXLongTask.rb', line 106 def __end! if @tracking RMXLongTask.outstanding_queue.sync do RMXLongTask.outstanding_tasks.delete(self) RMXLongTask.internal("check for all complete") do |internal_task| Dispatch::Queue.main.async do if RMXLongTask.outstanding_tasks.size.zero? RMX.new(RMXLongTask).trigger(:all_complete) end internal_task.end! end end end end if @bgTask && @bgTask != UIBackgroundTaskInvalid UIApplication.sharedApplication.endBackgroundTask(@bgTask) @bgTask = UIBackgroundTaskInvalid end end |
#end! ⇒ Object
99 100 101 102 103 104 |
# File 'lib/motion/RMXLongTask.rb', line 99 def end! if DEBUG_LONGTASK || @verbose p "SUCCESS: #{@desc} ended successfully." end __end! end |
#rmx_dealloc ⇒ Object
126 127 128 129 130 131 |
# File 'lib/motion/RMXLongTask.rb', line 126 def rmx_dealloc if DEBUG_DEALLOC p "DEALLOC: #{@desc}" end super end |