在iOS中异步调用数据库 [英] asynchronous calls to database in ios

查看:180
本文介绍了在iOS中异步调用数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的iPad应用4的UITableView的。
我加载使用函数 loadData ,这是存在于所有的4 TableViewController.m文件,这使得调用数据库对他们的数据。

I have 4 UITableView's on my iPAD application. I load the data on them using a function loadData ,which is there in all the 4 TableViewController.m files, which makes calls to the database.

所以,我会做这样的事情。

So, I would be doing something like this

[aView loadData];
[bView loadData];
[cView loadData];
[dView loadData];

在哪里aView,bView,CVIEW和DVIEW是的的UITableView的视图控制器。

Where aView, bView, cView and dView are the view controllers of the UITableView's.

但是,数据库调用同步发生,并且将数据从 [aView loadData] 函数检索因此,只有后,确实在 [bView loadData ] 函数被调用等。

However, the database calls happen synchronously and hence only after the data is retrieved from the [aView loadData] function, does the [bView loadData] function get called and so on.

这会影响我的表现。

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

I would like to know if there is a way I can asynchronously make calls to the database/ asynchronously make calls to functions which calls database.

这将是巨大的,如果有人可以帮助我了这一点。

It would be great if someone could help me out with this.

推荐答案

您可以使用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 法,activityIndi​​cator将启动动画,以及数据的提取过程将在一个单独的队列异步启动,但loadList方法将立即返回(不等待块 dispatch_async 执行完,这就是 dispatch_async 是)。

Then when you will call the loadList method, the activityIndicator will start animate, and the fetching process of your data will be started on a separate queue asynchronously, but the loadList method will return immediately (not waiting for the block in dispatch_async to finish executing, that's what dispatch_async is for).

因此​​,要在每个viewcontrollers你4 loadList 实现您所有的通话将立即执行,(触发您的数据读取异步而不是等待数据是检索)。一旦数据库请求 - 这是在并行执行队列 - 在你的 loadList 方法之一已经完成,在 dispatch_sync(...)在该块的结束线被执行,请求主队列(主线程)来执行一些code键刷新UI和显示新加载的数据

So all your call to your 4 loadList implementations in each of your viewcontrollers will execute immediately, (triggering the fetching of your data asynchronously but not waiting for the data to be retrieved). Once the database requests -- that was executing in a parallel queue -- have finished in one of your loadList method, the dispatch_sync(...) line at the end of the block is executed, asking the main queue (main thread) to execute some code to refresh the UI and display the newly-loaded data.

这篇关于在iOS中异步调用数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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