如何创建多个视图可以访问的数据源?
我当前的项目有TabBarController
链接到多个视图,所有视图都需要访问同一个好友列表。
目前,我将数据源存储在名为DataHolder
的单例类中,代码显示在下面。
DataHolder.m
#import "DataHolder.h"
@interface DataHolder(){
DataQuery *dataQuery;
NSMutableArray *friendList;
}
@end
@implementation DataHolder
+(DataHolder*)sharedInstance{
static DataHolder *_sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[DataHolder alloc]init];
});
return _sharedInstance;
}
-(id)init{
self = [super init];
if (self){
NSLog(@"DataHolder init method called");
dataQuery =[[DataQuery alloc]init];
}
return self;
}
-(NSMutableArray *)getFriendList{
NSLog(@"Friend List from DataHolder %@", [dataQuery getFriendList]);
return [dataQuery getFriendList];
}
-(void)logout{
[friendList removeAllObjects];
}
@end
我遇到的问题是我一次不能拥有多个视图来保存数据。我目前在每个viewController中使用NSMutableArray
属性并让它们调用self.Array = [NSMutableArray ArrayWithArray:[DataHolder SharedInstance]GetFriendList]];
这并不总是返回任何数据,特别是如果之前调用了另一个viewController。我也觉得令人费解的是,一些viewControllers将不显示数据,而另一个拥有数据,下次我运行应用程序时,它会有一个具有数据的不同viewController。
编辑:这是执行查询的dataQuery
类,DataHolder
保留在其中。
DataQuery.m
@interface DataQuery(){
NSMutableArray *friendsToAccept;
//NSMutableArray *friendsList;
int *count;
}
@end
@implementation DataQuery
-(id)init{
self = [super init];
if (self){
self.friendsList = [NSMutableArray new];
//[self.friendsList mutableArrayValueForKey:@"friendsArray"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getFriendList) name:@"friendList" object:nil];
self.currentUser = [[CurrentUser sharedInstance]getCurrentUser];
[self friendListQueryWithDataSource:self.friendsList];
//[self checkForFriends];
}
return self;
}
-(NSMutableArray *)getFriendList{
return self.friendsList;
}
-(void)friendListQueryWithDataSource:(NSMutableArray*)datasource{
PFRelation *friendRelation = [self.currentUser relationForKey:@"friendRelation"];
PFQuery *acceptedQuery = [friendRelation query];
acceptedQuery.cachePolicy = kPFCachePolicyCacheThenNetwork;
[acceptedQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error){
NSLog(@"ERROR: %@ %@", error, [error userInfo]);
}else{
self.friendsList = [NSMutableArray arrayWithArray:objects];
NSLog(@"Friend List from DataQuery %@", self.friendsList);
}
}];
}
以下是视图控制器如何设置以显示数据
#import "ConnectionTableViewController.h"
@interface ConnectionTableViewController (){
DataHolder *dataHolder;
}
@end
@implementation ConnectionTableViewController
- (void)viewDidLoad
{
self.pending = [NSArray new];
NSLog(@"%@", self.pending);
self.friendList = [NSMutableArray new];
self.edgesForExtendedLayout = UIRectEdgeNone;
backgroundQueue = dispatch_queue_create("word", NULL);
self.friendList = [NSMutableArray arrayWithArray:[[DataHolder sharedInstance] getFriendList]];
[super viewDidLoad];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"friendList"]) {
for (PFUser *user in [[DataHolder sharedInstance]getFriendList]){
[self.friendList addObject:user];
}
[self.tableView reloadData];
}
}
答案 0 :(得分:1)
使用NSNotificationCenter在数据下载完成后收到通知。在我看来,你使用Parse作为后端。
答案 1 :(得分:0)
findObjectsInBackgroundWithBlock:
是一种异步方法!在完成之前,您不会在friendList中拥有任何数据。
尝试使getFriendList
异步。
- (void)getFriendListWithCallback:(CallbackBlock)callback {
//...
if (self.friendsList.count == 0) {
[acceptedQuary findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error){
self.friendsList = @[];
NSLog(@"ERROR: %@ %@", error, [error userInfo]);
} else {
self.friendsList = [NSMutableArray arrayWithArray:objects];
NSLog(@"Friend List from DataQuary %@", self.friendsList);
}
if (callback) callback(self.friendsList);
}];
} else {
if (callback) callback(self.friendsList);
}
}