1

I'm taking my first swing at a Swift/NSOperationQueue based design, and I'm trying to figure out how to maintain data integrity across queues.

I'm early in the design process, but the architecture is probably going to involve one queue (call it sensorQ) handling a stream of sensor measurements from a variety of sensors that will feed a fusion model. Sensor data will come in at a variety of rates, some quite fast (accelerometer data, for example), but some will require extended computation that could take, say, a second or more.

What I'm trying to figure out is how to capture the current state into the UI. The UI must be handled by the main queue (call it mainQ) but will reflect the current state of the fusion engine.

I don't want to hammer the UI thread with every update that happens on the sensor queue because they could be happening quite frequently, so an NSOperationQueue.mainQueue.addOperationWithBlock() call passing state back to the UI doesn't seem feasible. By the same token, I don't want to send queries to the sensor queue because if it's processing a long calculation I'll block waiting for it.

I'm thinking to set up an NSTimer that might copy the state into the UI every tenth of a second or so.

To do that, I need to make sure that the state isn't being updated on the sensor queue at the same time I'm copying it out to the UI queue. Seems like a job for a semaphore, but I'm not turning up much mention of semaphores in relation to NSOperationQueues.

I am finding references to dispatch_semaphore_t objects in Grand Central Dispatch.

So my question is basically, what's the recommended way of handling these situations? I see repeated admonitions to work at the highest levels of abstraction (NSOperationQueue) unless you need the optimization of a lower level such as GCD. Is this a case I need the optimization? Can the dispatch_semiphore_t work with NSOperationQueue? Is there an NSOperationQueue based approach here that I'm overlooking?

4
  • You may want to watch developer.apple.com/videos/play/wwdc2015/226 for ideas, also there are some libs based on this talk. May 5, 2016 at 5:34
  • You'll probably won't need semaphores at all here. You can try to add UI refresh operation as a dependency to long-running one, AFAIK dependencies can be cross-queue, also as a dependency to e.g. every 10th fast refreshing operation. Also try to avoid timers, they may have very negative impact on battery life. May 5, 2016 at 5:39
  • What's the best way to set up the dependencies that won't violate the MVC architecture? Sensor data goes straight to the sensorQ which feeds the model. Shouldn't the UI pull data from the model, rather than make the model aware of the view controller? Perhaps notifications are the way to go? (I'm still watching that video-- thanks for the pointer).
    – Omegaman
    May 5, 2016 at 6:41
  • Model definitely shouldn't know about UI. Some variant of observer should be enough(notifications/delegation/callback block). May 5, 2016 at 8:26

2 Answers 2

0

How much data are you sending to the UI? A few numbers? A complex graph?

If you are processing all your sensor data on an NSOperationQueue (let's call it sensorQ), why not make the queue serial? Then when your timer fires, you can post an "Update UI" task to sensorQ. When your update task arrives on the sensorQ, you know no other sensor is modifying the state. You can bundle up your data and post to the main (UI) queue.

A better answer could be provided if we knew: 1. Do your sensors have a minimum and maximum data rate? 2. How many sensors are contributing to your fusion model? 3. How are you synchronizing access from the sensors to your fusion model? 4. How much data and in what format is the "update" to the UI?

My hunch is, semaphores are not required.

1
  • Is avoiding semaphores the preferred idiom with NSOperationQueues? The sensor complement isn't fully determined yet, but the extremes mentioned above would be accelerometer data at 100-200Hz, and image-like data that might take a second to process down. The later argues against putting UI requests into sensorQ. sensorQ will be serial, ensuring that sensor updates are FIFO. Data to the UI will be a set of numbers in production, but perhaps quite a bit more for diagnostics (not as critical). Graphing could be done by retaining time series in the UI controller.
    – Omegaman
    May 5, 2016 at 5:15
0

One method that might work here is to decouple the sensor data queue from your UI activities via a ring buffer. This effectively eliminates the need for semaphores.

The idea is that the sensor data processing component pushes data into the ring buffer and the UI component pulls the data from the ring buffer. The sensor data thread writes at the rate determined by your sensor/processing and the UI thread reads at whatever refresh rate is appropriate for your application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.