Uitableview ios中对数据库的异步调用

Uitableview ios中对数据库的异步调用,uitableview,core-data,asynchronous,ios4,Uitableview,Core Data,Asynchronous,Ios4,我的iPAD应用程序上有4个UITableView。 我使用函数loadData加载数据,该函数位于所有4个TableViewController.m文件中,用于调用数据库 所以,我会这样做 [aView loadData]; [bView loadData]; [cView loadData]; [dView loadData]; 其中,aView、bView、cView和dView是UITableView的视图控制器 但是,数据库调用是同步进行的,因此只有在从[aView loadData

我的iPAD应用程序上有4个UITableView。 我使用函数
loadData
加载数据,该函数位于所有4个TableViewController.m文件中,用于调用数据库

所以,我会这样做

[aView loadData];
[bView loadData];
[cView loadData];
[dView loadData];
其中,aView、bView、cView和dView是UITableView的视图控制器

但是,数据库调用是同步进行的,因此只有在从
[aView loadData]
函数检索数据之后,
[bView loadData]
函数才会被调用,依此类推

这影响了我的表现

我想知道是否有一种方法可以异步调用数据库/异步调用调用数据库的函数


如果有人能帮我解决这个问题,那就太好了。

您可以使用GCD来解决这个问题:

-(void)loadList
{
   // You ma do some UI stuff
   [self.activityIndicator startAnimating]; // for example if you have an UIActivityIndicator to show while loading

   // Then dispatch the fetching of the data from database on a separate/paralle queue asynchronously (non-blocking)
   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

      // this is executed on the concurrent (parallel) queue, asynchronously
      ... do your database requests, fill the model with the data

      // then update your UI content.
      // *Important*: Updating the UI must *always* be done on the main thread/queue!
      dispatch_sync(dispatch_get_main_queue(), ^{
          [self.activityIndicator stopAnimating]; // for example
          [self.tableView reloadData];
      });
   });
}
然后,当调用
loadList
方法时,activityIndicator将启动animate,数据的提取过程将在单独的队列上异步启动,但loadList方法将立即返回(不要等待
dispatch\u async
中的块完成执行,这就是
dispatch\u async
的作用)


因此,对每个ViewController中的4
loadList
实现的所有调用都将立即执行(异步触发数据的获取,但不等待数据被检索)。在并行队列中执行的数据库请求在
loadList
方法中完成后,将执行块末尾的
dispatch\u sync(…)
行,询问主队列(主线程)要执行一些代码以刷新UI并显示新加载的数据,可以使用GCD:

-(void)loadList
{
   // You ma do some UI stuff
   [self.activityIndicator startAnimating]; // for example if you have an UIActivityIndicator to show while loading

   // Then dispatch the fetching of the data from database on a separate/paralle queue asynchronously (non-blocking)
   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

      // this is executed on the concurrent (parallel) queue, asynchronously
      ... do your database requests, fill the model with the data

      // then update your UI content.
      // *Important*: Updating the UI must *always* be done on the main thread/queue!
      dispatch_sync(dispatch_get_main_queue(), ^{
          [self.activityIndicator stopAnimating]; // for example
          [self.tableView reloadData];
      });
   });
}
然后,当调用
loadList
方法时,activityIndicator将启动animate,数据的提取过程将在单独的队列上异步启动,但loadList方法将立即返回(不要等待
dispatch\u async
中的块完成执行,这就是
dispatch\u async
的作用)


因此,对每个ViewController中的4
loadList
实现的所有调用都将立即执行(异步触发数据的获取,但不等待数据被检索)。在并行队列中执行的数据库请求在
loadList
方法中完成后,将执行块末尾的
dispatch\u sync(…)
行,询问主队列(主线程)执行一些代码以刷新UI并显示新加载的数据。

我已经编辑了答案并添加了一些详细信息。是的,您应该在每个
loadData
函数中添加这段代码,这会更好(或者您可以改为使用
dispatch\u async(…)括起
loadData
调用)调用方代码中的
,但这不是很好和可读的,ViewController负责触发其关联视图的更新,因此调用
[self.tableView reloadData]
)就是他们的工作。关于第二个问题:我的代码中的主队列是使用
dispatch\u get\u main\u queue()检索的
功能。有关Grand Central Dispatch(GCD)的更多信息,请阅读&dispatch queuesit工作得很好。我想问另一件重要的事情。在我的loadData函数中,我有对Web服务的调用。因此,我要做的是,如果数据库中没有任何内容,我会调用Web服务。当使用此异步函数时,会调用WS,但在这之后不会发生任何事情。我做错了什么?y怎么办使用
NSURLConnection
及其异步API进行Web服务调用?如果是这样,这是一个好的解决方案,但要知道,当使用
NSURLConnection
connectionWithRequest:
启动请求时,该请求将被安排在调用它的线程的nsrunlop上,以便正确处理uess这就是您的下载没有发生的原因(如果它以前工作过的话)…因此我想基本上您应该设置一个runloop,或者更简单地在主线程上调用您的Web服务(如果您使用
NSURLConnection的异步API
,这不是一个问题)是的,我使用的是NSURLConnection。同样,当你说在主线程上调用你的Web服务时,这意味着在dispatch_async的结束括号之后。对吗?我已经编辑了我的答案并添加了一些细节。是的,你应该在每个
loadData
函数中添加这段代码,这样更好(或者您可以在调用方代码中使用
dispatch_async(…)
loadData
调用括起来,但这不是很好和可读的,ViewController负责触发其关联视图的更新,因此调用
[self.tableView reloadData]
就是他们的工作)。关于第二个问题:我的代码中的主队列是使用
dispatch\u get\u main\u queue()
函数检索的。有关Grand Central dispatch(GCD)的更多信息,请阅读&dispatch queuesit工作得很好。我想问另一件重要的事情。在我的loadData函数中,我有对Web服务的调用。因此,我要做的是,如果数据库中没有任何内容,我会调用Web服务。当使用此异步函数时,会调用WS,但在这之后不会发生任何事情。我做错了什么?y怎么办使用
NSURLConnection
及其异步API进行Web服务调用?如果是这样,这是一个好的解决方案,但要知道,当使用
NSURLConnection
connectionWithRequest:
启动请求时,该请求将被安排在调用它的线程的nsrunlop上,以便正确处理uess这是您的下载没有发生的原因(如果是