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) ⇒ LongTask

Returns a new instance of LongTask.



39
40
41
42
43
44
45
46
# File 'lib/motion/util.rb', line 39

def initialize(desc=nil)
  @desc = "#{rmext_object_desc} #{desc}"
  @bgTask = UIApplication.sharedApplication.beginBackgroundTaskWithName(@desc, expirationHandler:lambda do
    p "ERROR: #{@desc} didn't call #end! in time!"
    __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

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

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



33
34
35
36
37
# File 'lib/motion/util.rb', line 33

def self.create(desc=nil, &block)
  x = new(desc)
  block.weak!.call(x)
  x
end

.time_remainingObject



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

def self.time_remaining
  UIApplication.sharedApplication.backgroundTimeRemaining
end

Instance Method Details

#__end!Object



55
56
57
58
59
60
# File 'lib/motion/util.rb', line 55

def __end!
  if @bgTask && @bgTask != UIBackgroundTaskInvalid
    UIApplication.sharedApplication.endBackgroundTask(@bgTask)
    @bgTask = UIBackgroundTaskInvalid
  end
end

#deallocObject



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

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

#end!Object



48
49
50
51
52
53
# File 'lib/motion/util.rb', line 48

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