Tag: asynchronous

sendAsynchronousRequest使UI冻结

downloadImages是一个button,每当我按下它,一个微调应该开始滚动,一个asynchronous请求应谷歌(以确保有一个连接),并收到响应后,我开始同步下载图像。 不知何故,微调不会去,似乎请求是同步,而不是asynchronous。 – (IBAction)downloadImages:(id)sender { NSString *ping=@"http://www.google.com/"; GlobalVars *globals = [GlobalVars sharedInstance]; [self startSpinner:@"Please Wait."]; NSURL *url = [[NSURL alloc] initWithString:ping]; NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5.0]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if (data) { for(int i=globals.farmerList.count-1; i>=0;i–) { //Definitions NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; […]

当我在asynchronous重载数据期间滚动时,为什么我的UITableView失去stream动性?

这是我的问题…我正在对我正在使用的服务进行asynchronous调用,它将search结果返回给我。 完成后,重新载入我的表,并完成asynchronous。 问题是,当我尝试在asynchronous调用完成之前在我的表视图上滚动时,该表失去了所有滚动视图所具有的stream畅感。 我的意思是,当你放开卷轴而不是继续前进,慢慢停下来的时候,只要你放开,它就会停下来。 唯一的方法来恢复stream体的感觉是滚动一直到UITableView的顶部或底部和一点点过去,那就没事了。 这是我的asynchronous调用代码: __block NSMutableArray *appendMoreObjects = [[NSMutableArray alloc] init]; dispatch_async( dispatch_get_main_queue(), ^{ self.activity.hidden = NO; appendMoreObjects = [service search:self.query from:((moreCount * 8)) until:((moreCount * 8) + 8)]; if( appendMoreObjects == nil ) { //alert view } else { [self.allSearchResults addObjectsFromArray:appendMoreObjects]; [self updateSearchArray]; self.moreCount += 1; } self.activity.hidden = YES; }); 另外,请记住,我正在使用自定义的UITableViewCell。 […]

观察asynchronous请求

我有三个独立的API调用。 当所有三个调用完成后,我将汇总数据并使用它来形成一个单独的模型对象。 我想我会用财产观察员来做到这一点,但我不知道它将如何实施。 任何帮助或指导将不胜感激。 我创build了一个模型对象来进行networking调用,将响应数据传递给一个转义闭包。 这是parsing数据的function: func loadLibrary() { // League Data Containers var names = Dictionary<Int, String>() // API Call 1 var titles = Dictionary<Int, String>() // Call 1 var masteryLevels = Dictionary<Int, Int>() // 2 var masteryPoints = Dictionary<Int, Int>() // 2 // Champion Data Containers var championRolesLibrary = Array<Dictionary<String,Array<Role>>>() // 3 var […]

Swift iOS:收到远程通知后,Closure不会在后台运行

我正在写一个iOS应用程序,要求在收到推送通知时更新设备的GPS位置。 我使用闭包来获取当前的GPS位置。 这个代码在应用程序处于前台时完美运行(“新的远程通知”和“获得位置”都被打印到控制台),但是当应用程序在后台时,只有“新的远程通知”(并且没有位置错误)被打印到控制台。 因此,我现在还没有设备的GPS位置,这对于我的应用程序的function非常重要。 任何帮助将不胜感激。 谢谢。 我在Info.plist文件中有'必需的背景模式'; App registers for location updates App downloads content from the network App downloads content in response to push notifications 我的应用程序还可以随时访问位置,包括背景(在代码中的其他位置成功testing过):NSLocationAlwaysUsageDescription位于Info.plist文件中 在我的AppDelegate文件中: func application(application: UIApplication!, didReceiveRemoteNotification userInfo:NSDictionary!) { println("New remote notification") var notification:NSDictionary = userInfo as NSDictionary var loc: LocationManager? loc = LocationManager() loc!.fetchWithCompletion {location, error in // fetch […]

把sharedLoader函数转换成一个正常的函数

我有一个imageLoader函数,加载图像,而其他代码保持运行。 现在我希望同一个函数在所有其他代码保持运行之前先运行。 这是function: //调用函数 ImageLoader.sharedLoader.imageForUrl(urlstring as String, completionHandler:{(image: UIImage?, url: String) in self.productImageView.image = image! }) //函数本身 func imageForUrl(urlString: String, completionHandler:(image: UIImage?, url: String) -> ()) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {()in var data: NSData? = self.cache.objectForKey(urlString) as? NSData if let goodData = data { let image = UIImage(data: goodData) dispatch_async(dispatch_get_main_queue(), {() in completionHandler(image: image, url: urlString) […]

在Swift 2中创buildASYNC任务

我想在地图上显示用户当前的位置,我知道这不是即时的任务。 我想在ASYNC任务中调用我的showCurrentLocation()函数。 我试图学习callback闭包,但我不明白如何创buildASYNC任务。 我知道这不是一个最好的问题模板为stackoverflow,但我不知道我怎么能不同地问。 感谢您的任何帮助。 有一个很好的编码。

swift:asynchronous任务+完成

我很快就对asynchronous任务感到困惑。 我想要做的是这样的事情… func buttonPressed(button: UIButton) { // display an "animation" tell the user that it is calculating (do not want to freeze the screen // do some calculations (take very long time) at the background // the calculations result are needed to update the UI } 我试图做这样的事情: func buttonPressed(button: UIButton) { let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, […]

核心数据加载Swift后asynchronous刷新UITableView

我有一个UITableView, tViewNews 我有一个刷新function,从我的服务器下载数据,将其存储到核心数据,然后表视图从核心数据加载这些数据。 它完美的作品 func refresh(refreshControl: UIRefreshControl) { self.newsArray = [NewsItem]() self.newslists = [[NewsItem]]() self.getNewsFromServer() self.getNewsFromCoreData() self.tViewNews.reloadData() refreshControl.endRefreshing() } 现在,当用户第一次打开新闻viewDidLoad() ,我希望表视图首先从核心数据加载,然后从服务器asynchronous填充我的表视图数组(使用与刷新函数完全相同的方法)和然后重新加载表视图。 所以我已经在我的viewDidLoad()函数中尝试了下面的代码。 override func viewDidLoad() { super.viewDidLoad() // Getting news from Core Data getNewsFromCoreData() // Updating core data from server and populating table view from core data dispatch_async(dispatch_get_main_queue()) { self.newsArray = [NewsItem]() self.newslists = [[NewsItem]]() […]

在objective-c中使用asynchronous代码避免嵌套块

我需要在我的Objective-C代码中发生很长的一系列事件。 假设我有6件东西 – thingA,thingB,thingC,thingD,thingE和thingF。 thingB和thingD返回一个BOOL。 如果thingB是NO,那么thingC不需要被调用。 如果thingD是NO,那么thingE不需要被调用。 – (void)doThings:(void(^)())completion { [self thingA: ^{ [self thingB: ^(BOOL success) { if (success) { [self thingC: ^{ [self thingD: ^(BOOL success) { if (thingD) { [self thingE: ^{ [self thingF]; completion(); }]; return; } [self thingF]; completion(); }]; }]; return; } [self thingD: ^(BOOL success) { if (success) […]

等待asynchronous请求结果

我想以某种方式asynchronousvalidation在ABPadLockScreen引脚,因为引脚没有保存在设备上。 我使用Alamofire和PromiseKit一起提供http请求来承诺。 我试图使用AwaitKit但问题是,我陷入僵局。 我也试图使用semaphore ,但结果是一样的。 因为我不能改变ABPadLock方法来容纳像完成处理程序这样的东西,所以我需要一些解决scheme,不pipe它是否阻塞主线程,只要它工作。 Alamofire请求方法: public func loginAsync(pinCode: String?, apiPath: String?) -> Promise<LoginResult>{ return Promise { fullfil, reject in let params = [ "Pin": pinCode! ] Alamofire.request(.POST, "\(baseUrl!)/\(apiPath!)", parameters: params).responseObject{(response: Response<LoginResult, NSError>) in let serverResponse = response.response if serverResponse!.statusCode != 200 { reject(NSError(domain: "http", code: serverResponse!.statusCode, userInfo: nil)) } if let loginResult = […]