Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of iAsync library usage to wrap data acquisition. ( library link : https://github.com/EmbeddedSources/iAsync/ )
-(void)clientCode
{
JFFAsyncOperation dataLoader_ = [ builder buildDataLoaderWithAuthentication ];
dataLoader_( nil, nil, ^void( id result, NSError* error )
{
// your data is ready. Show it to the user
} );
}
// ==============
-(JFFAsyncOperation)buildDataLoaderWithAuthentication
{
return sequenceOfAsyncOperations( [ self asyncAuthentication ], [ self buildDataLoader ], nil );
}
-(JFFAsyncOperation)buildDataLoader
{
//// some initialization code
JFFSyncOperation syncDbFetch_ = ^id( NSError** errorPtr_ )
{
return [ database_ doSomeQuery: someQueryData_
error: errorPtr_ ];
};
JFFAsyncOperation asyncDbFetch_ = asyncOperationWithSyncOperation( syncDbFetch_ )
JFFAsyncOperationBinder dbImport_ = ^JFFAsyncOperation( id dataToImport )
{
JFFSyncOperation syncDbImport_ = ^id( NSError** errorPtr_ )
{
if ( [ database_ importData: dataToImport
error: errorPtr_ ] )
{
return [ NSNull null ];
}
return nil;
};
return asyncOperationWithSyncOperation( syncDbImport_ )
}
JFFURLConnectionParams* params_ = [ JFFURLConnectionParams new ];
params_.url = url_;
// more download params setup
JFFAsyncOperation download_ = dataURLResponseLoader( params_ );
JFFAsyncOperation downloadAndImport_ = bindSequenceOfAsyncOperations( download_, dbImport_, nil );
JFFAsyncOperation dataFromInet_ = sequenceOfAsyncOperationsArray( @[downloadAndImport_, asyncDbFetch_ ] );
JFFAsyncOperation tryCacheFirst = trySequenceOfAsyncOperations( asyncDbFetch_, dataFromInet_, nil );
return tryCacheFirst;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment