Asynchronous methods in RubyCocoa - objective-c

I understand that it isn't possible/sensible to use threads in RubyCocoa. However it is possible to use asynchronous Cocoa methods to avoid blocking user interface events.
I've successfully used a method on NSURLConnection to send an HTTP request and receive the response without blocking the user interface. But I'm wondering what other asynchronous Cocoa methods like this are available?
Also is it possible/sensible within a RubyCocoa application to use Ruby to spawn separate processes (as opposed to threads)? I suppose one issue would be how to wait for the process to complete, but perhaps this could be done by polling via NSTimer events?

Check this client, it's written in ruby and works pretty well.
httpclient

Related

Can a C++20 co-routine implement a synchronous interface without blocking?

I am writing a GRPC service and am trying to use the asynchronous methods with the help from Asio. The service calls into a C++ library that have synchronous methods. The code in that library uses interfaces that must be implemented by the user of the library. These interfaces contains synchronous methods.
I wish to implement these interfaces by using asynchronous GRPC calls to other services. My challenge is that I cannot see how I can implement an adaptor between the synchronous - and asynchronous world. Is this at all possible in C++?
In (my) theory I want this co-routine adaptor to send the GRCP request and then the thread should continue executing other co-routines - and not be blocking - while waiting for the GRPC reply. When the reply is received the synchronous method call is returned to the library. This way I would be able to implement my GRPC service with only one thread and I do not have to worry about multi-threading issues.
When using co_await in a method then the return value is reflecting the async nature of the method, so I cannot use co_await (directly) when implementing a synchronous interface. Instead I can post a lambda containing the co_wait, but then I have to do a blocking wait on a future (or similar) and my single threaded service is deadlocked. I have been thinking of using co_yield and make a type of generator since it seems to be that the consumer of these generators can be synchronous.
Best regards

Multiple NSURLConnection running background

I am trying to send multiple NSUrlConnection simultaneously using thread concept. Also i am trying to handle the connection delegates.
I tried sending repeated request but i dono how to handle the received data. Is there any documentation regarding this multiple NSURLConnection.
Use sendAsynchronousRequest:queue:completionHandler: to send a request in the background and use to completion handler to react on the completed handler. This will allow you to send as much requests as you want at the same time.
For a documentation about NSURLConnection have a look at the official Apple Class Reference and Samples.
I would also recommend you do have a look at ASIHTTPRequest or MKNetworkKit. Both are libraries supporting you to create HTTP requests, but with a lot of useful additions and a little bit easier to use.

Asynchronous NSUrlConnection not on main runloop

I am creating a set of classes which interface with a web service. At the core of this, the data is retrieved from the service using an asynchronous NSUrlConnection. In my mind, it is important that it is asynchronous, as a client of these web service interfaces has to have the ability to cancel a request that is in progress (i.e. cancel an NSUrlConnection).
The web service calls return JSON data, potentially lots of it, and this is parsed and the classes I am creating will create proper data structures out of them. Depending on which web service method is called, the request can end up being expensive - too expensive to run on the main thread, so I would like to either add the option of running the service requests asynchronously, or not giving the option, and forcing asynchronous calls.
Async calls are all well and good, but I am having problems starting an NSUrlConnection asynchronously on a runloop that isn't the main one. The problem I'm describing seems to be fairly well documented: I am led to believe the delegate of the NSUrlConnection is not called because the runloop that launches the connection has terminated, and therefore the calls back to the delegate cannot be scheduled on its runloop.
What is the best way to go about solving this issue?
I have tried using:
while (!self.isRequestComplete && !self.isRequestCancelled)
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
It seems to work ok from the basic trial I have done, except if the runloop that this is being executed on is actually the main runloop, for which I have had a few crashes...
Would an option be to offer asynchronous calls to clients, and then use the above method if the option is utilised? Is there a better way of achieving what I am trying to do?
What I am aiming to achieve is for a package of classes that allow interfacing with my specific web service, where the clients of my code do not need to worry about whether their own delegates (which my classes hold references to) will be called on different threads. I want them to be called on the exact same runloop that they called my code on - basically, exactly how NSUrlConnection operates!
Thanks in advance!
Nick
I think you may have "gone up the wrong creek" so to speak. Generally speaking you don't need to worry about run loops unless you are doing something rather odd. It sounds like you need to do some reading on multi-threading, particularly Grand Central Dispatch.

Is having a function call block a bad design process?

I'm writing an API which is used to receive some data from another application. Currently the function is designed to block until data is received. In my mind this limits developers using the API to use multithreading or some sort of multi-process design. So is it better for a function to block or to return a null and then sleep for a few milliseconds before trying again.
Note the other application may not have any data to send through the API for an unknown period of time.
The API is written in C++
Why not use a callback?
You could define the API to allow the user to pass an optional timeout value. If the timeout is not specified, then the API function waits indefinitely, much like how select() works.
Consider another option: use an async transaction -> issue a request & provide a callback address with ticket id. When the response is available, the service end-point callbacks your application with the ticket id and of your the result ;-)
You should avoid as must as possible blocking when you possibly can.
As you say:
Note the other application may not have any data to send through the API for an unknown period of time.
In this case, using a synchronous interface ties up resources unnecessarily.
You haven't said what language this is, but it sounds like your API is listening or checking for some event, and the users of the API are either blocking or polling your API to determine if the event happened?
Is it possible to use a callback? Users of the API would register for notifications of the event happening, and when your library detects the event it will use the callback to notify all listeners.
When your applications calls the O/S api function read(), do you expect it to block? Of course you do—at least by default. In some circumstances, ioctl's allow a programmer to change the behavior to be asynchronous, which is particularly common in network applications.
You've shed very little light on what your API is about, so consider:
Does it make sense that an API user would want to be blocked? That is, is there little to do until it returns.
If you were writing an application for the API, what would you expect it to do? You should definitely write a few sample applications for your own education, as well as to document the API.
Is there any reason why the API user would not multithread (or fork, etc.) requests to the API?
If you want a reusable solution you could apply the Asynchronous Design 'Pattern' which is common in .NET but can also be implemented in C++ as demonstrated in this CodeProject project.
There's nothing wrong with providing both synchronous and asynchronous calls to the same feature in the interface.
Personally I would only go these lengths if I need to service multiple requests (in which case you can queue 'BeginOperation' requests for example), or there are many potentially asynchronous operations in the interface (and I want a standardised, flexible pattern). If you can only handle one request at a time a time-out is usually sufficient.

Silverlight WCF Proxy async only?

Why do the Silerlight-generated WCF proxy class(es) offer only async calls?
There are cases where I don't really need the async pattern (for example in a BackgroundWorker)
EDIT : Sometimes I need to process the results of two WCF calls. It would have been much simpler if I could have waited (the business of the app allows that) for both calls to end and then process.. but noooo.... async! :P
As I understand it, the aim here is to make it hard for people to do the wrong thing (sync. IO from the UI). If you are using the WCF classes, you'll probably have to live with it.
There's actually a technical reason you can't do sync calls, at least from the 'main' browser thread, which is that the browser invokes all the plug-in API calls on the same thread, so if SL were to block that thread while waiting for the network callback, the network callback wouldn't get through and the app would deadlock. That said, the sync API would work fine if initiated from a different thread -- ie, if the application first does a QueueUserWorkItem to get off the browser thread -- but we felt it would be confusing to offer the sync option and have it only work some of the time.
Andrei, there ar emethods that even using the async pattern, allows you write expressive code, esasy to read and maintian, without becoming crazy wating 4 async requests, by just simplifying the way you write your code.
give a look to this library http://syncwcf.codeplex.com/