从ios本机代码访问存储在AsyncStorage中的数据(目标c) [英] Access data stored in AsyncStorage from ios native code (objective c)

查看:63
本文介绍了从ios本机代码访问存储在AsyncStorage中的数据(目标c)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从iOS本机Objective C代码访问存储在AsyncStorage中的数据.

I need to access data stored in AsyncStorage from iOS native Objective C code.

需要这样做以使数据同步,而不是将App事件发送到JS,然后再将其发送回本机代码.

This is needed to get the data in sync instead of sending App event to JS and then send it back to native code.

推荐答案

我刚刚遇到了同样的问题.

I've just been faced with the same problem.

我的解决方案是移动代码本机.

My solution was to move the code native side.

在iOS上:

#import <React/RCTAsyncLocalStorage.h>
#import <React/RCTBridgeModule.h>

RCTResponseSenderBlock completion = ^(NSArray *response) {
  NSString *theme = response[1][0][0][1];

  // Setup RCTRootView with initialProperties
};

RCTAsyncLocalStorage *storage = [[RCTAsyncLocalStorage alloc] init];

dispatch_async(storage.methodQueue, ^{
  [storage performSelector:@selector(multiGet:callback:) withObject:@[@"theme"] withObject:completion];
});

您还可以使用dispatch_semaphore_wait同步执行此操作

You could additionally use dispatch_semaphore_wait to perform this synchronously

更新:

我需要全局状态的变量,而不仅仅是组件props中的变量,因此上述内容还不够完善.

I needed the variable in the global state not just in the component props so the above doesn't go far enough.

在加载Javascript源时,我不得不将它处理到React Native源中.

I've had to work this into the React Native source at the point that the Javascript source is loaded.

NSString *javascriptPrepend = [NSString stringWithFormat:@"var ThemeMode = '%@';", self.theme];
NSMutableData *prependData = [[javascriptPrepend dataUsingEncoding:NSUTF8StringEncoding] mutableCopy];
[prependData appendData:sourceCode];

sourceCode = prependData; 

我会看看他们是否愿意向PR开放以允许这种功能,如果获得通过,请在此处发回邮件.

I'll see if they're open to a PR to allow this kind of functionality and post back here if I get it through.

这篇关于从ios本机代码访问存储在AsyncStorage中的数据(目标c)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆