Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
iOS Objective C native code using React Native ASYNCSTORAGE.
#import <React/RCTAsyncLocalStorage.h>
- (void)jsonFromLocalRNStrogeForKey:(NSString *)key completion:(void (^)(NSDictionary * _Nullable, NSError * _Nullable))completion {
RCTResponseSenderBlock rnCompletion = ^(NSArray *response) {
NSString *jsonAsString = @"";
if (response.count > 1) {
NSArray *response1 = response[1];
if (response1.count > 0) {
NSArray *response2 = response1[0];
if (response2.count > 1) {
jsonAsString = response2[1];
}
}
}
if (![jsonAsString isEqual: [NSNull null]] && [jsonAsString length] > 0) {
NSData *jsonAsData = [jsonAsString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:jsonAsData
options:NSJSONReadingMutableContainers
error:&error];
completion(json, error);
} else {
completion(@{}, nil);
}
};
RCTAsyncLocalStorage *storage = [RCTAsyncLocalStorage new];
dispatch_async(storage.methodQueue, ^{
[storage performSelector:@selector(multiGet:callback:) withObject:@[key] withObject:rnCompletion];
});
}
- (void)stringFromLocalRNStrogeForKey:(NSString *)key completion:(void (^)(NSString*, NSError * _Nullable))completion {
RCTResponseSenderBlock rnCompletion = ^(NSArray *response) {
NSString *jsonAsString = @"";
if (response.count > 1) {
NSArray *response1 = response[1];
if (response1.count > 0) {
NSArray *response2 = response1[0];
if (response2.count > 1) {
jsonAsString = response2[1];
}
}
}
if (![jsonAsString isEqual: [NSNull null]] && [jsonAsString length] > 0) {
completion(jsonAsString, nil);
} else {
completion(@"", nil);
}
};
RCTAsyncLocalStorage *storage = [RCTAsyncLocalStorage new];
dispatch_async(storage.methodQueue, ^{
[storage performSelector:@selector(multiGet:callback:) withObject:@[key] withObject:rnCompletion];
});
}
- (void)saveDictionaryToLocalRNStrogeWithKey:(NSString *)key data: (NSDictionary*) data completion:(void (^)(Boolean, NSError * _Nullable))completion {
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data
options:kNilOptions
error:&error];
if (jsonData) {
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSArray * dataToSave = @[@[key, jsonString]];
RCTResponseSenderBlock rnCompletion = ^(NSArray *response) {
completion(true, nil);
};
RCTAsyncLocalStorage *storage = [RCTAsyncLocalStorage new];
dispatch_async(storage.methodQueue, ^{
[storage performSelector:@selector(multiSet:callback:) withObject:dataToSave withObject:rnCompletion];
});
}
}
[self stringFromLocalRNStrogeForKey:@"@Beek:user_id" completion:^(NSString* userId, NSError * _Nullable errorUserId) {
if ([userId length] != 0) {
[self jsonFromLocalRNStrogeForKey:@"audiobooks_map" completion:^(NSDictionary* audiobooksMap, NSError * _Nullable errorAudiobooksMap) {
if ([playingChapter.audiobookID length] != 0) {
[self jsonFromLocalRNStrogeForKey:@"audiobook_progresses_map" completion:^(NSDictionary* audiobookProgressesMap, NSError * _Nullable errorProgressesMap) {
// DO STUFF HERE.
}];
}
}];
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.