iOS SDK  3.5.0.104555
 All Classes Files Functions Variables Properties Macros Pages
Multi-Threaded Environments

The Aspera Mobile SDK for iOS is thread-safe when used correctly. ASTransferQueue methods are thread-safe except where otherwise noted. Several notes pertaining to the thread safety of ASAccount and ASFaspSession instances are noted here.

Models: One Instance Per Thread

In order to guarantee thread safety when dealing with model objects such as ASAccount and ASFaspSession instances, the SDK maintains a separate instance for each thread that needs to interact with any given session or account - that is to say, there may be multiple instances of ASAccount representing any one given account, one per thread (and the same goes for ASFaspSession). This has several implications which are detailed in the remainder of this page.

Passing Instances Between Threads

One should avoid passing account or session instances between threads. This is an important point, so it will be repeated: do not pass account or session instances between threads.

In order to work around this limitation, you can pass the "object ID" of a particular account or session between threads, and then use that object ID to retrieve the correct instance for the thread you are in. Object IDs may be retrieved via the method objectID:

Objective-C     Swift
NSManagedObjectID *objID = [account objectID];
let objID = account.objectID

You can then pass this object ID to another thread, and use it to retrieve an ASAccount instance representing the same account in this other thread:

Objective-C     Swift
// In another thread, having in some
// fashion received objID
ASAccount *account = [ASAccount accountWithID:objID];
// In another thread, having in some
// fashion received objID
let account = ASAccount(ID: objID)

Similar code can be used with ASFaspSession instances.

Refreshing Models with Changes From Other Threads

If a model instance is changed in a particular thread, those changes may not be reflected in the instances that represent that same model object in other threads. Most API methods are guaranteed to return up-to-date model objects, but under some circumstances it may be necessary to force an instance to load the latest changes made to the object in any other threads. This may be done by invoking the refresh method on that object, see refresh (ASAccount) and refresh (ASFaspSession).

It is important to note that refreshing a model instance discards any unsaved changes to that model made in the calling thread.

Saving Changes From a Particular Thread

Changes made to session or account instances hold only in the thread in which they were made. To cause the changes to propagate to other threads the instances must be saved.

Most ASTransferQueue methods that act on sessions and accounts save the objects they are passed, so the user need not be bothered with this task. If however you modify a session or account outside of the context of these classes you will need to save the changes yourself. You may do this by invoking ASFaspSession::save or save (ASAccount), but note that these methods raise an exception if the object is not valid, therefore you should validate the object before saving - see Model Validation for more information.