Skip to content

Instantly share code, notes, and snippets.

@umhan35
Last active July 14, 2016 03:10
Show Gist options
  • Save umhan35/fd0e0c9efab7b92380868e4d51c46808 to your computer and use it in GitHub Desktop.
Save umhan35/fd0e0c9efab7b92380868e4d51c46808 to your computer and use it in GitHub Desktop.
access asyncLocalStorage in objective-c
// add libReact.a
// import
#import "RCTAsyncLocalStorage.h"
// code
RCTAsyncLocalStorage *asyncLocalStorage = [[RCTAsyncLocalStorage alloc] init];
dispatch_async([asyncLocalStorage methodQueue], ^{
[asyncLocalStorage multiGetKeys:@[@"widget_city"] callback:^(NSArray *response){
NSLog(@"%@", response);
dispatch_async(dispatch_get_main_queue(), ^{
[self populateViewForCity];
});
}];
});
- (void)multiGetKeys:(NSArray<NSString *> *)keys callback:(RCTResponseSenderBlock)callback;
- (void)multiGetKeys:(NSArray<NSString *> *)keys
callback:(RCTResponseSenderBlock)callback
{
NSDictionary *errorOut = [self _ensureSetup];
if (errorOut) {
callback(@[@[errorOut], (id)kCFNull]);
return;
}
NSMutableArray<NSDictionary *> *errors;
NSMutableArray<NSArray<NSString *> *> *result = [[NSMutableArray alloc] initWithCapacity:keys.count];
for (NSString *key in keys) {
id keyError;
id value = [self _getValueForKey:key errorOut:&keyError];
[result addObject:@[key, RCTNullIfNil(value)]];
RCTAppendError(keyError, &errors);
}
callback(@[RCTNullIfNil(errors), result]);
}
#pragma mark - Exported JS Functions
RCT_EXPORT_METHOD(multiGet:(NSArray<NSString *> *)keys
callback:(RCTResponseSenderBlock)callback)
{
[self multiGetKeys:keys callback:callback];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment