问题描述
首先,我是一个很新的目标C /可可iOS开发,但我已经写了C / C ++应用程序之前。
First of all, I am a a very new Objective C/Cocoa iOS Developer but I've written C/C++ applications before.
所以我设法运行的RabbitMQ-C(http://hg.rabbitmq.com/rabbitmq-c/)客户端我的iPhone应用程序里面,我可以连接直接从我的应用程序的RabbitMQ,甚至消耗队列项。因此,所有的好。
So I managed to run the Rabbitmq-c (http://hg.rabbitmq.com/rabbitmq-c/) client inside my iPhone App, and I can connect to Rabbitmq directly from my app and even consume a queue item. So all's good.
现在我的问题是,我的iPhone应用程序需要使用RabbitMQ的-C库轮询从服务器收到的消息。可能会出现,一个几乎无限while循环。
Now my problem is, my iPhone app needs to use the rabbitmq-c library to poll for incoming messages in from the server. Probably there will be, an almost infinite while loop.
我必须拿这个去一个新线程?理想情况下,我想换行的RabbitMQ-C类作为一个异步目标C类,并使用NSNotification(或类似的东西)来通知我的UI。我有点戒心创建一个新的线程,因为我读到像Runloop等东西可以解决很多问题,而无需使用额外的线程。
Do I have to take this to a new thread? Ideally, I want to wrap the rabbitmq-c class as an Async objective C class and use NSNotification (or something similar) to notify my UI. I'm a bit leery of creating a new thread, as I read about stuffs like Runloop etc can solve a lot of problems without using an extra thread.
什么是我去对此的最好方法? code或方向的任何例子将是有益的。请记住,我不是在处理一个目标C code /可口可乐RabbitMQ的图书馆在这里,我使用C code我的iPhone应用程序中。
What is the best way for me to go about this? Any examples of code or directions would be helpful. Please remember, I am not dealing with an Objective C code/Coca rabbitmq library here, I'm using C code inside my iPhone app.
感谢
Subrat
推荐答案
不会阻止您的服务器轮询的主线。
don't block the main thread with your server polling.
由于操作永远不会结束,创建此服务器轮询自己的线程和运行循环。你可能会使用运行循环(每个线程都有一个),而不是无限的一段时间。替代方案涉及定期产卵线程。最简单的方法只使用一个线程这一点。
since the operation never ends, create your own thread and run loop for this server polling. you can potentially use the run loop (each thread has one) instead of the infinite while. the alternatives involve regularly spawning threads. it's easiest to just use one thread for this.
一旦你有一个更新,发布通知(如果您选择NSNotification)从主线程 - UIKit的是为了从主线程操作仅
once you have an update, post the notification (if you choose NSNotification) from the main thread -- UIKit is meant to operate from the main thread only.
样品,我就开始与NSRunLoop和CFRunLoop样本。
for samples, i'd begin with samples related to NSRunLoop and CFRunLoop.
好运气
这篇关于在目标C运行C code(可可)螺纹(适用于iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!