How should I design notifications in Cocoa if I plan to optimize for concurrency later? - objective-c

In my app, I want to create a class that receives a certain type of notifications, begins it's work and sends out notifications when it's done. I think that later I may need to use concurrency to optimize the app — so this work that the class does is done in separate threads — but right now I don't have any knowledge or experience of working with concurrency and I don't want to spend time on premature optimizaion. However, if I understand correctly, the default usage of notifications doesn't mix with concurrency so well.
Is there a way that I can just follow few simple rules with notifications right now without diving into concurrency, and avoid rewriting all that code later?

Yes, you can avoid a rewrite.
I would write your work/background tasks inside blocks and use GCD (Grand Central Dispatch). This works fine and is easy to use in the non-parallel case, but will also allow you to easily parallelize your work later.
I'd look into NSBlockOperation and NSOperationQueue and/or dispatch_async()

Related

How to perform queries in background thread?

I'm currently looking at having a KMM application backed by SQLdelight for all domain-related operations.
SQLdelight seems to provide really nice interfaces, however it seems like all the write calls (insert/update/delete) are implemented using blocking calls, so I'm worried that it would hurt the responsiveness of the app by blocking the main thread a lot.
Is there a recommended way to perform such operations without blocking the main thread?
The app would have to work on iOS as well, so I can't afford freezing too much.
A bit late to answer but it might be useful for others:
You should use wiwthContext(Dispatchers.Default) assuming you are using the native-mt version of coroutine libraries. That allow you to ensure insert/update/delete are not executed on the main thread.
You also have the possibility of using sqldelight coroutine-extensions library to return a flow from your queries to observe changes in your database.

Rewriting a threaded Objective-C 'story engine' in CoffeeScript: How to script actions & conditions sequentially without a ton of callbacks?

I have been experimenting with porting the underlying 'story engine' of my Objective-C iPhone adventure Scarlett and the Spark of Life to HTML5 using CoffeeScript (and I am looking into IcedCoffeeScript).
The graphical part can just use DIVs on the DOM — the requirements there are fairly simple. The problematic part is the 'command and control' story-type commands. The ideal is to be able to express high-level story commands — including conditionals — and have them executed sequentially. So, for example, in faux-CoffeeScript:
scarlett.walkTo(200,300)
scarlett.turnTo(0)
story.wait(0.8)
if interesting
scarlett.think('Looks interesting.')
else
scarlett.think('Looks boring.')
In Objective-C (this was back when scripting languages like Lua were banned on the App Store), we achieved this by having two threads. The main thread ran cocos2d-phone which handled all the OpenGL calls, animation and other cocos niceties. The 'story' thread handled the command-and-control of the story, and if necessary the thread would sleep, awaiting an NSCondition before returning from a function and proceeding to the next call.
It sounds awkward, but it allowed us to express story commands and conditionals in a sequential, natural way, just using normal-looking code. Note that in the example above, the if check for the variable interesting would be evaluated right before Scarlett says something, not at the start of the function. Also, the walkTo(), turnTo(), wait() and think() calls will not return until their associated animation, delay or text box is finished back on the main thread.
What I'm struggling with is how to achieve this expressiveness using web technologies. As I see it, my options are:
Using a Web Worker as the story 'thread'. However, as far as I'm aware, workers can't sleep, and state isn't shared so they can't even perform a busy wait.
Using a callback chain, probably utilising IcedCoffeeScript's await and defer keywords to keep the code tidier. Even with those, though, that's a lot of extra line noise.
Somehow evaluate lines from the story script one-by-one as strings. I can't help feeling that it would be highly problematic.
(Similar in some ways to 3.) Write the story commands in a specially-designed interpreted language, where the program counter could be stopped and started as needed. It seems like this is unnecessarily re-inventing the wheel.
I can't help feeling like I'm overlooking some really obvious solution, though. Am I looking at this back-to-front, somehow? Is there an acknowledged pattern for scripting sequential actions and conditionals over time using actual code, without a mountain of callbacks?

Synchronization and threading for an agent-based modeling project in Objective-C

First of all, I'm an Objective-C novice. Most of my background is in Java.. Also since most Objective-C questions revolve around Cocoa, I should point out that this is on GNUStep.
For a school project, I'm creating a simple agent-based-modeling framework. These frameworks are typically used to model complex systems (like the spreading of diseases). My framework features two main objects: a world and a bug. The world consists of "layers", each of which is associated with a toroidal grid. The world can be populated by bugs, and each bug has an x and a y coordinate, and a layer that it belongs to.
My general idea is to populate the world with bugs, and then fire of threads for each of the bugs and let them do what they want. You can create any kind of bug by subclassing the main Bug class and by implementing an act method defined in a protocol. This way you can have various types of custom bugs and custom behavior. Bugs should be able to interact with the world and each other (removing bugs from the world, adding bugs to the world, moving itself around). As you can see, this is quickly headed to multi-threading hell.
Currently I have a bunch of #synchronized blocks and I'm having a hard time ensuring that the world always remains in a consistent state. This is made especially difficult since the bug needs to communicate with and act on the world and vice-versa. I am trying to implement a simple bug called a RandomBug that randomly moves around the world. Even this is proving to be difficult because I'm seeing potential problems where state can become corrupted or invalid.
I started taking a look at NSOperation and NSOperationQueue because it appears that this might make things easier. I have two questions pertaining to this:
Is there an easy way to perform NSOperations repeatedly (i.e., at specific intervals).
If I set the number of maximum concurrent operations on the thread to 1, do I still need #synchronized blocks? Wouldn't only one thread be interacting with the world at a given time?
Is there a better way to tackle this sort of problem (multiple threads interacting with one shared resources in a repeated manner)?
Should I forgo threading altogether and simply iterate through bugs on the world and activate them in a random manner?
Sounds like you might want something ala a game/simulation loop... So you have an "update world" phase of your run loop for each time step of your simulation (triggered by an NSTimer), where each bug gets a chance to interact with the world; repeat. Unless your bugs are CPU intensive, this might be the way to go....
As for using NSOperation--sure, this will potentially let you use all your CPU cores, however if there's lots of contention for accessing the world state, this may not be a win after all. In that case, you might try making each tile of your world a separate object you can #synchronized against, reducing contention and allowing better use of your CPU(s).
Using a single NSOperationQueue and setting maxConcurrentOperations = 1 is the same as implementing a game loop, basically. If you use an NSOperationQueue, but don't set maxConcurrentOperations, I'd expect NSOperationQueue to run as many operations simultaneously as you have CPU cores.

When Would Anyone Want To Use NSThreads over the GCD?

Are there any cases when anyone would want to use raw NSThreads instead of GCD for concurrency? I love the GCD, but I want to know if I will need to use NSThreads for Cocoa/Cocoa-Touch eventually.
i use pthreads for control, good performance, and portability. sometimes, you might opt to use NSThread for the extra NSObject interfacing it offers.
there are a few lower level interfaces where you need to coordinate threads with the APIs you use (e.g. realtime I/O or rendering). sometimes you have flexibility regarding the thread you use, sometimes it is convenient to use NSThread in this situation so you can easily use CF or NS run loops with these interfaces. So the run loop parameter you set up on your thread is likely of more interest to the API than the thread itself. in these cases, GCD may not necessarily be an alternative.
but… most devs won't need to drop to these levels often.
You should essentially almost never need to use the NSThread/pthread APIs directly on OS X or iOS. On other platforms, possibly yes (though GCD is becoming more widely ported to *BSD, Linux and even Windows - see Wikipedia page for Grand Central Dispatch), but on Apple OS platforms you're almost always going to get a better result by allowing the system to do thread lifecycle management for you. The only case where you might conceivably want to do your own thread management are in highly real-time scenarios where you need to manage thread priorities and have direct control over thread latency by balancing the amount of work each thread is doing by hand.
There may be some special situations where you have to do something strange that cannot be done with GCD. But anything that you can do with GCD you should do it that way (GCD and threads are not mutually exclusive, if you need to actually use a thread you need not change any of the GCD stuff you already have).
Not sure however what the case would be. Maybe if you need to setup a secondary specialized RunLoop (not sure if it can be done with GCD but surely it can with a thread). Or there may be some other special case I cannot figure at the moment.

What would a multithreaded UI api look like, and what advantages would it provide?

Or, equivalently, how would you design such an API. Expected/example usage would be illustrative as well.
My curiosity comes directly from the comments (and subsequent editting on my part) of this answer. Similar questions/discussions in the past provide a bit of inspiration to actually asking it.
Executive summary:
I don't feel a multithreaded UI api is possible in a meaningful way, nor particularly desirable. This view seems somewhat contentious and being a (relatively) humble man I'd like to see the error of my ways, if they actually are erroneous.
*Multithreaded is defined pretty loosely in this context, treat** it however makes sense to you.
Since this is pretty free-form, I'll be accepting whichever answer has the most coherent and well supported answer in my opinion; regardless of whether I agree with it.
Answer Accepted
**Ok, perhaps more clarification is necessary.
Pretty much every serious application has more than one thread. At the very least, they'll spin up an additional thread to do some background task in response to a UI event.
I do not consider this a multithreaded UI.
All the UI work is being done on single thread still. I'd say, at a basic level, a multithreaded UI api would have to do away with (in some way) thread based ownership of UI objects or dispatching events to a single thread.
Remeber, this is about the UI api itself; not the applications that makes use of it.
I don't see how a multithreaded UI API would differ much from existing ones. The major differences would be:
(If using a non-GC'd language like C++) Object lifetimes are tracked by reference-counted pointer wrappers such as std::tr1::shared_ptr. This ensures you don't race with a thread trying to delete an object.
All methods are reentrant, thread-safe, and guaranteed not to block on event callbacks (therefore, event callbacks shall not be invoked while holding locks)
A total order on locks would need to be specified; for example, the implementation of a method on a control would only be allowed to invoke methods on child controls, except by scheduling an asynchronous callback to run later or on another thread.
With those two changes, you can apply this to almost any GUI framework you like. There's not really a need for massive changes; however, the additional locking overhead will slow it down, and the restrictions on lock ordering will make designing custom controls somewhat more complex.
Since this usually is a lot more trouble than it's worth, most GUI frameworks strike a middle ground; UI objects can generally only be manipulated from the UI thread (some systems, such as win32, allow there to be multiple UI threads with seperate UI objects), and to communicate between threads there is a threadsafe method to schedule a callback to be invoked on the UI thread.
Most GUI's are multithreaded, at least in the sense that the GUI is running in a separate thread from the rest of the application, and often one more thread for an event handler. This has the obvious benefit of complicated backend work and synchronous IO not bringing the GUI to a screeching halt, and vice versa.
Adding more threads tends to be a proposition of diminishing returns, unless you're handling things like multi-touch or multi-user. However, most multi-touch input seems to be handled threaded at the driver level, so there's usually no need for it at the GUI level. For the most part you only need 1:1 thread to user ratio plus some constant number depending on what exactly you're doing.
For example, pre-caching threads are popular. The thread can burn any extra CPU cycles doing predictive caching, to make things run faster in general. Animation threads... If you have intensive animations, but you want to maintain responsiveness you can put the animation in a lower priority thread than the rest of the UI. Event handler threads are also popular, as mentioned above, but are usually provided transparently to the users of the framework.
So there are definitely uses for threads, but there's no point in spawning large numbers of threads for a GUI. However, if you were writing your own GUI framework you would definitely have to implement it using a threaded model.
There is nothing wrong with, nor particularly special about multithreaded ui apps. All you need is some sort of synchronization between threads and a way to update the ui across thread boundaries (BeginInvoke in C#, SendMessage in a plain Win32 app, etc).
As for uses, pretty much everything you see is multithreaded, from Internet Browsers (they have background threads downloading files while a main thread is taking care of displaying the parts downloaded - again, making use of heavy synchronization) to Office apps (the save function in Microsoft Office comes to mind) to games (good luck finding a single threaded big name game). In fact the C# WinForms UI spawns a new thread for the UI out of the box!
What specifically do you think is not desirable or hard to implement about it?
I don't see any benifit really. Let's say the average app has 3 primary goals:
Rendering
User input / event handlers
Number crunching / Network / Disk / Etc
Dividing these into one thread each(several for #3) would be pretty logical and I would call #1 and #2 UI.
You could say that #1 is already multithreaded and divided on tons of shader-processors on the GPU. I don't know if adding more threads on the CPU would help really. (at least if you are using standard shaders, IIRC some software ray tracers and other CGI renderers use several threads - but i would put such applications under #3)
The user input metods, #2, should only be really really short, and invoke stuff from #3 if more time is needed, that adding more threads here wouldn't be of any use.