Class: RMExtensions::LongTask

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/util.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(desc = nil, verbose = false, tracking = true) ⇒ LongTask

Returns a new instance of LongTask.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/motion/util.rb', line 88

def initialize(desc=nil, verbose=false, tracking=true)
  @verbose = verbose
  @tracking = tracking
  @desc = "#{rmext_object_desc} #{desc}"
  @bgTask = UIApplication.sharedApplication.beginBackgroundTaskWithExpirationHandler(lambda do
    p "ERROR: #{@desc} didn't call #end! in time!"
    __end!
  end)
  if ::RMExtensions.debug? || @verbose
    p "CREATED: #{@desc}"
  end
  if @tracking
    ::RMExtensions::LongTask.outstanding_queue.sync do
      ::RMExtensions::LongTask.outstanding_tasks << self
    end
  end
  self
end

Instance Attribute Details

#bgTaskObject

Returns the value of attribute bgTask.



26
27
28
# File 'lib/motion/util.rb', line 26

def bgTask
  @bgTask
end

#descObject

Returns the value of attribute desc.



26
27
28
# File 'lib/motion/util.rb', line 26

def desc
  @desc
end

Class Method Details

._creator(desc = nil, verbose = false, tracking = true, &block) ⇒ Object



72
73
74
75
76
# File 'lib/motion/util.rb', line 72

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

RMExtensions::BackgroundTask.create(“my long task”) { |task| task.end! }



64
65
66
# File 'lib/motion/util.rb', line 64

def self.create(desc=nil, &block)
  _creator(desc, false, true, &block)
end

.internal(desc = nil, &block) ⇒ Object



68
69
70
# File 'lib/motion/util.rb', line 68

def self.internal(desc=nil, &block)
  _creator(desc, false, false, &block)
end

.outstanding_queueObject



44
45
46
47
48
49
# File 'lib/motion/util.rb', line 44

def self.outstanding_queue
  Dispatch.once do
    @outstanding_queue = Dispatch::Queue.new("#{NSBundle.mainBundle.bundleIdentifier}.outstanding.LongTask")
  end
  @outstanding_queue
end

.outstanding_tasksObject



51
52
53
54
55
56
# File 'lib/motion/util.rb', line 51

def self.outstanding_tasks
  Dispatch.once do
    @outstanding_tasks = []
  end
  @outstanding_tasks
end

.reset_outstanding_tasks!Object



78
79
80
81
82
83
84
85
86
# File 'lib/motion/util.rb', line 78

def self.reset_outstanding_tasks!
  ::RMExtensions::LongTask.outstanding_queue.sync do
    size = ::RMExtensions::LongTask.outstanding_tasks.size
    if size > 0
      p "WARNING: reset_outstanding_tasks! (was: #{size})"
      ::RMExtensions::LongTask.outstanding_tasks.removeAllObjects
    end
  end
end

.time_remainingObject



28
29
30
# File 'lib/motion/util.rb', line 28

def self.time_remaining
  UIApplication.sharedApplication.backgroundTimeRemaining
end

.verbose(desc = nil, &block) ⇒ Object

RMExtensions::BackgroundTask.verbose(“my long task”) { |task| task.end! }



59
60
61
# File 'lib/motion/util.rb', line 59

def self.verbose(desc=nil, &block)
  _creator(desc, true, true, &block)
end

.when_all_complete(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/motion/util.rb', line 32

def self.when_all_complete(&block)
  rmext_on_main_q do
    if ::RMExtensions::LongTask.outstanding_tasks.size.zero?
      rmext_block_on_main_q(block)
    else
      ::RMExtensions::LongTask.rmext_once(:all_complete) do |opts|
        block.call
      end
    end
  end
end

Instance Method Details

#__end!Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/motion/util.rb', line 114

def __end!
  if @tracking
    ::RMExtensions::LongTask.outstanding_queue.sync do
      ::RMExtensions::LongTask.outstanding_tasks.delete(self)
      ::RMExtensions::LongTask.internal("check for all complete") do |internal_task|
        rmext_on_main_q do
          if ::RMExtensions::LongTask.outstanding_tasks.size.zero?
            ::RMExtensions::LongTask.rmext_trigger(:all_complete)
          end
          internal_task.end!
        end
      end
    end
  end
  if @bgTask && @bgTask != UIBackgroundTaskInvalid
    UIApplication.sharedApplication.endBackgroundTask(@bgTask)
    @bgTask = UIBackgroundTaskInvalid
  end
end

#deallocObject



134
135
136
137
138
139
# File 'lib/motion/util.rb', line 134

def dealloc
  if ::RMExtensions.debug?
    p "DEALLOC: #{@desc}"
  end
  super
end

#end!Object



107
108
109
110
111
112
# File 'lib/motion/util.rb', line 107

def end!
  if ::RMExtensions.debug? || @verbose
    p "SUCCESS: #{@desc} ended successfully."
  end
  __end!
end