Notifier早先调用预期

时间:2017-03-04 14:22:54

标签: ios objective-c xcode uitabbarcontroller

我有以下问题

0) Homepage having two button, Button A , Button B
1) TabbarviewC.h/TabbarviewC.m  //set in storyboard class its a subclass of UITabBarController

- (void)viewDidLoad 
{
[self loadAPI];   
}

    -(void)loadAPI
    {

     //load my api
     //on success load another api and insert data in sqlite database
     //on completion of insert database i add notifier 

    [self databaseinsertComplete];
    }

-(void)databaseinsertComplete
{

  [[NSNotificationCenter defaultCenter]  postNotificationName:@"DataLoadedCompletely" object:nil];


}

2)我在TabbarviewC.m中加载了一个API     3)我有4个标签(tab1,tab2,tab3,tab4)与TabbarviewC链接

现在,根据情况,我需要在所有4个标签中传递数据 还

If user Press button A -> it load the API and then jump to tab 1 

-(IBAction)ButtonA:(id)sender
{
UIStoryboard *storyboard  = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UITabBarController *tbc = [storyboard instantiateViewControllerWithIdentifier:@"TabBarController"];
            tbc.selectedIndex=0;
            self.window.rootViewController = tbc;
            [UIView transitionWithView:self.window duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:nil];
}


If user press button B -> it will open the tab2

-(IBAction)ButtonB:(id)sender
{
UIStoryboard *storyboard  = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UITabBarController *tbc = [storyboard instantiateViewControllerWithIdentifier:@"TabBarController"];
            tbc.selectedIndex=1;
            self.window.rootViewController = tbc;
            [UIView transitionWithView:self.window duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:nil];
}

在我的tab1或tab 2中,我添加了通知程序addObserver来调用reload tableview函数,但是在数据插入sqlite db之前的早期调用

- (void)viewDidLoad
{
 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reloadTableview)
                                                 name:@"DataLoadedCompletely" object:nil];
} 

-(void) reloadTableview
{
//my table view reload
//it loads the data from database and then reload the tableview
}

在这两种情况下我需要在TabbarviewC上加载API数据,并且在成功时我必须在选项卡1或选项卡2中显示数据

在我的情况下,API响应稍后会出现,我的标签会在第一时间显示空的tableview结果。

2 个答案:

答案 0 :(得分:1)

NSNotificationCenter同步发布通知。不确定如何将数据保存到sqlite数据库,但如果你有完成处理程序 - 你应该用它来发布通知。此外,这里的好处是与NSNotificationQueue异步发布通知。这样的事情:

NSNotification *notification = [NSNotification notificationWithName:@"DataLoadedCompletely" 
                                                             object:nil];
[[NSNotificationQueue defaultQueue] enqueueNotification:notification
                                           postingStyle:NSPostASAP];

通过这种方式,您可以及时收到通知,并且您没有竞争条件。

答案 1 :(得分:1)

最有可能的是,无论你正在做什么来检索你的数据都是异步运行的......但你发送的是#34;完成" 开始数据检索过程后立即通知。

-(void)loadAPI
{

 //load my api
 //on success load another api and insert data in sqlite database
 //on completion of insert database i add notifier 

[self goGetData]; // probably returns immediately

[self databaseinsertComplete]; // method is called *while* data is still being retrieved
}

所以,如果你的"获取数据"进程异步,请确保在发送" DataLoadedCompletely"之前等待它完全完成。通知。