问题描述
我有两个类名为 InterfaceController
和 LoadInterfaceController
。
我在 LoadInterfaceController
中获取信息,我想传递给变量
code> InterfaceController 类。
LoadInterfaceController
:
- (void)fetchData {
NSArray * database = //获取的信息
// ... on fetch complete
InterfaceController * interfaceController = [InterfaceController alloc];
[interfaceController initilizeDatabase:database];
}
InterfaceController
:
@property(nonatomic)NSArray * db;
- (void)initilizeDatabase:(NSArray *)database {
self.db = database;
NSLog(@database count:%d,database.count); //:3
NSLog(@db count:%d,self.db.count); //:3
[self utilInformation];
}
- (void)util信息{
NSLog(@db count:%d,self.db.count); //:0
// ...
}
db
的信息不保留在 initilizeDatabase:
函数之外。
主线程上运行它(如解释),但是信息仍然没有保留。
线程没有问题,保持,一切都是正确的从这个角度。但是还有另一个问题:当你以编程方式初始化 InterfaceController
时,你创建了一个实例,并且 db
存储直到你的Watch重新加载,因为Watch自己创建另一个 InterfaceController
的实例,结果你得到空的 db
属性。
我建议的解决方案是在您的架构中添加一个模型实体,作为单例类,并确保您的
下面是一个考虑您的示例的实现:
XYZDataBaseManager
:
@interface XYZDataBaseManager:NSObject
+(nonnull XYZDataBaseManager *)sharedInstance;
- @property(nonnull,strong)NSMutableArray * data;
@end
@implementation XYZDataBaseManager
+(XYZDataBaseManager *)sharedInstance {
static dispatch_once_t once;
static XYZDataBaseManager * instance;
dispatch_once(& once,^ {
instance = [[XYZDataBaseManager alloc] init];
});
return instance;
}
@end
LoadInterfaceController
:
- (void)fetchData {
NSArray * database = // fetched信息
[XYZDataBaseManager sharedInstance] .data = database;
}
InterfaceController
:
@property(nonatomic)NSArray * db;
- (void)util信息{
NSLog(@db count:%d,[XYZDataBaseManager sharedInstance] .data.count]);
}
当然有很多事情需要改进,但我认为主要思想清楚了。而且我没有执行这段代码,所以可能会有一些打字错误(我更加面向快速)。
I have two classes named InterfaceController
and LoadInterfaceController
.
I'm fetching information in the LoadInterfaceController
that I wish to pass-on to a variable
in my InterfaceController
class.
LoadInterfaceController
:
- (void)fetchData {
NSArray *database = //fetched information
//... on fetch complete
InterfaceController *interfaceController = [InterfaceController alloc];
[interfaceController initilizeDatabase:database];
}
InterfaceController
:
@property (nonatomic) NSArray *db;
- (void)initilizeDatabase:(NSArray *)database {
self.db = database;
NSLog(@"database count: %d", database.count); //: 3
NSLog(@"db count: %d", self.db.count); //: 3
[self utilizeInformation];
}
- (void)utilizeInformation {
NSLog(@"db count: %d", self.db.count); //: 0
//...
}
db
's information is not retained outside of the initilizeDatabase:
function.
I have tried running it on the main thread
(as explained here), but the information is still not retained. How may I specify the thread to pass-on the information from my second class?
There are no problems with the threads or retention, everything is right from this perspective. But there is another issue: when you initialise the InterfaceController
programmatically you create an instance of it and the db
property is perfectly stored until your Watch loads it again, because Watch creates another instance of InterfaceController
on its own as a result you get the empty db
property on a new instance.
The solution I'd suggest here is to add a model entity into your architecture that will act as a singleton class and it ensures your data always will be available.
Here is an implementation considering your example:
XYZDataBaseManager
:
@interface XYZDataBaseManager : NSObject
+ (nonnull XYZDataBaseManager *)sharedInstance;
- @property (nonnull, strong) NSMutableArray *data;
@end
@implementation XYZDataBaseManager
+ (XYZDataBaseManager *)sharedInstance {
static dispatch_once_t once;
static XYZDataBaseManager *instance;
dispatch_once(&once, ^{
instance = [[XYZDataBaseManager alloc] init];
});
return instance;
}
@end
LoadInterfaceController
:
- (void)fetchData {
NSArray *database = //fetched information
[XYZDataBaseManager sharedInstance].data = database;
}
InterfaceController
:
@property (nonatomic) NSArray *db;
- (void)utilizeInformation {
NSLog(@"db count: %d", [XYZDataBaseManager sharedInstance].data.count]);
}
Of course there are a lot of things to improve but I think the main idea is clear. And also I didn't execute this code so there might be some typos (I'm more swift-oriented).
这篇关于WatchKit问题在类之间保留数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!