如何等待调用来初始化Rubymotion中的变量?
我们可以让某个进程等待一个变量被初始化,然后继续吗 我有以下代码:如何等待调用来初始化Rubymotion中的变量?,ruby,multithreading,rubymotion,Ruby,Multithreading,Rubymotion,我们可以让某个进程等待一个变量被初始化,然后继续吗 我有以下代码: def viewDidLoad BW::Location.get(distance_filter: 10, desired_accuracy: :best) do |result| if result[:error].nil? lat = result[:to].latitude lng = result[:to].longitude end BW:
def viewDidLoad
BW::Location.get(distance_filter: 10, desired_accuracy: :best) do |result|
if result[:error].nil?
lat = result[:to].latitude
lng = result[:to].longitude
end
BW::HTTP.get("APIURL" |response|
case response.status_code
when 200
parsed_json = BW::JSON.parse(response.body)
if parsed_json[:tags]
App::Persistence['tag'] = parsed_json[:tags]
else
UIApplication.sharedApplication.delegate.logged_out
end
end
end
end
def imagePickerController(picker, didFinishPickingMediaWithInfo: info)
begin
App.alert('hello')
end while App::Persistence['tag'].nil?
end
App.alert("Finish")
end
拍照时,我的App::Persistence['tag']可能还没有收到API调用的值。因此,当用户单击“下一步”按钮时,我想让他们等待App::Persistence['tag']初始化后再继续
这似乎不起作用,它会继续警告“finish”。一个简单的解决方案是将后期拾取/相机业务逻辑放入自己的方法中,然后当图像拾取器/相机完成时,如果标签准备好了,调用该业务逻辑方法,如果还没有准备好,则设置KVO以监听标签的更改,并存储来自选择器/摄像机的信息,然后一旦KVO检测到标记已就绪,它将调用您的业务逻辑方法(或调用您的业务逻辑方法的方法)
您可以添加更多代码来显示哪个线程应该等待,哪个线程负责初始化吗?多线程是很难的。没有上下文的多线程更难。嗨@Chandranshu,你理解我的场景吗?如果我错了,请纠正我:当有人单击“下一步”按钮时,你正在服务器上进行API调用,你想给他们看一个旋转的轮子,表示他们需要等待。是吗?@Chandranshu是的……所以我实际上在等待App::Persistence['tag']初始化。你需要使用dispatch_信号量使你对API的调用同步。有关一些代码,请参阅。如果这不能解决你的问题,请告诉我。
attr_accessor :cameraInfo
def doTheThing
# your business logic, do something with self.cameraInfo
self.cameraInfo = nil
end
def imagePickerController(picker, didFinishPickingMediaWithInfo: info)
self.cameraInfo = info
if App::Persistence['tag'].nil?
# setup KVO or some basic loop in another thread or something along those lines
else
doTheThing
end
end