Using singleton in other thread - localization

I should use the play.i18n.Messages localisation service in an other thread.
I can use the service in the other thread but the text value doesn't get returned just the given key as like the key would exist in the messages file.
I souppose that the play.i18n.Messages is a singleton. Is this the problem? Is there a way to get the original service which is working in the parent thread.

Related

How to copy values out of a Realm Object to pass to another thread

I have a Realm Object (let's call it File) with a String property called url.
I have created an Alamofire DownloadRequest.DownloadFileDestination block which contains a reference to the url so I can download the file. However, because this block gets executed on a background thread, Realm throws up an exception. I don't need to actually access the entire Realm, or even the entire File object - only the property url - so there is no reason to try to open the File object from the Realm in the background thread.
What is the proper way to copy this string (or properties of other types) out of the Realm object to pass to a different thread?
My current solution seems very inelegant - though it does work.
let url = "\(object.url)"
This question is not related to a specific block of code - it is more a conceptual question.
This is normal, if you read the Realm's docs for threading, it said you cannot have multiple threads sharing the same instances of Realm objects, that mean you fetch the File object on the main thread, you cannot use it on other thread, in your case is Alamofire background thread
What you did is correct, assigning the value of url to another variable, else, just call fetchFile().url where fetchFiles() return the file object from Realm (refetch the file object - but this will have worse performance, only useful in other case where the realm object changed a lot)
Simply assigning let url = object.url will copy the value and is thread safe.

Can I use an ignored property across threads with Realm?

For an object shared between threads (via persisting and querying), will changes to an ignored property made in one thread be visible in another thread?
To share objects between threads or re-use them between app launches you must persist them to a Realm ... all changes you make to it will be persisted (and must be made within a write transaction). Any changes are made available to other threads that use the same Realm when the write transaction is committed.
http://realm.io/docs/cocoa/0.91.1/#writes
It looks like this doesn't apply to ignored properties. Each thread's instance of the object has its own copy of the ignored property, and changes in one thread don't affect any other threads. Is that right?
Correct. When you access an RLMObject from another thread by re-querying for it, it will be a new instance of the object, so the ignored properties will not be carried along with that one.
That being said, as long as you don't try and access any of the Realm-backed properties (Else a RLMException will be triggered), you CAN pass an RLMObject instance from one thread to another and still continue to access its ignored properties on the new thread.

On iOS, how to check inside a new thread for a UISwitch's value in the ViewController's view?

On iOS, if there is a single view app, and a new thread is created using:
[NSThread detachNewThreadSelector:#selector(consumeData:)
toTarget:self.consumer withObject:self.queue];
where the consumer is a Consumer object that will process data inside the method consumeData, and the queue is a Queue object, which is where the data comes from for the consumer to process.
But what if the thread needs to check whether a Switch on the main view is set to on or off? That is to toggle whether the Consumer object should do the work or pause at the moment. Should the withObject:self be used instead, so that the whole ViewController reference is passed to the thread, and then the thread will use viewController.view.______ to access the switch's value, and use viewController.queue to access the queue, or is there a better or alternative method?
Absolutely not. Nothing UI-related can ever be touched from another thread. It's simply not safe. If the other thread needs to know the switch's current value, then it needs to call back to the main thread before asking for it.
If you create a subclass, you could store your state in variables in the object, then access these variables from any thread; provided of course these operations do not call methods defined by UIKit.

How does pthread_key_t and the method pthread_key_create work?

I am having some trouble figuring out how pthread_key_t and pthread_key_create work. From my understand, each thread has TLS (thread local storage) and that a key is used to access the thread local storage. What I do not get is when a key is created, does every thread get to use it? Lets say Thread 0 creates key 0, can Thread 1 then use key 0? If Thread 1 used key 0, would it access its own TLS or Thread 0's TLS?
Is there some global array or something that keeps track of all the keys being used?
pthread_keys are just what you said, thread local storage referred to by a common key. So multiple threads use the same key, but get different storage space (per thread).
A quick example (contrived too), say you were building an asynchronous server (like IMAP). You could keep track of client connections in an array, with each having a key for the current task/request. So when a request comes in a new thread is spun up and the thread stores in the Client_Connection->WhatAmIDoing key a pointer to the "request" structure. The thread now wouldn't have to pass around that pointer because any function that thread executes could simply call the pthread_getspecific() function and get the pointer to what it's supposed to be doing.

call delegate method if property changes

I use RestKit with Object-mapping. This runs asyncronus and after receiving the Data from Server an object is updated.
The point is, i need to inform another class that a property of an object has changed.
Right now i run into an error:
bool _WebTryThreadLock(bool), 0x1ae420: Tried to obtain the web
lock from a thread other than the main thread or the web thread.
This may be a result of calling to UIKit from a secondary thread. Crashing now...
I tried to overwrite the setter for the property, but it looks like the property is set in another thread, not the main thread. calling a delegate there does not work.
What can i do to solve this?
Any help is appreciated!
Maybe you need send message like this:
[obj performSelectorOnMainThread:#selector(method)]

Resources