Skip to content

Instantly share code, notes, and snippets.

@dodikk
Last active December 15, 2015 02:19
Show Gist options
  • Save dodikk/5186878 to your computer and use it in GitHub Desktop.
Save dodikk/5186878 to your computer and use it in GitHub Desktop.
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