Tag: grand central dispatch

关于调用dispatch_queue_t和dispatch_sync

为什么需要代码if (dispatch_get_current_queue() == socketQueue) ? 为什么我们不能直接使用dispatch_sync(socketQueue, block) ? 提前致谢! – (BOOL)isConnected { __block BOOL result = NO; dispatch_block_t block = ^{ result = (flags & kConnected) ? YES : NO; }; if (dispatch_get_current_queue() == socketQueue) block(); else dispatch_sync(socketQueue, block); return result; } 顺便说一句,代码是从XMPPFramework

如何使用调度gcd与NSOperationQueue和NSBlockOperation?

这是代码 @interface ViewController () @property (nonatomic, strong) NSOperationQueue *queue; @end @implementation ViewController – (void)viewDidLoad { [super viewDidLoad]; _queue = [[NSOperationQueue alloc] init]; NSBlockOperation *aBlockOperation = [[NSBlockOperation alloc] init]; __weak NSBlockOperation* aWeakBlockOperation = aBlockOperation; [aBlockOperation addExecutionBlock:^{ NSLog(@"queue should still have the operation. And it does. yay!: %@", [_queue operations]); // This should print correctly. It will […]

在iOS中高效同步数据

我正在开发应用程序,将数据同步到服务器和从服务器同步,当应用程序进入后台,应用程序变为活动状态。 因为当我同步到多个设备,数据应该在所有设备上更新。 所以这里是我如何做到这一点, – (void)applicationDidBecomeActive:(UIApplication *)application { if([[AppManager instance].globalManager activeSyncCounter] == 0) { [_webService callServerAPI]; } } – (void)applicationDidEnterBackground:(UIApplication *)application { if([[AppManager instance].globalManager activeSyncCounter] == 0) { [_webService callServerAPI]; } } 让我来解释一下上面的代码 1)我正在调用API来同步我的数据,我遍历for循环来同步我的数据并将其发送到服务器(所有的调用都是asynchronous的) 2)你可以看到我已经使用了activeSyncCounter 。 我使用过这个,因为用户可以立即打开和退出应用程序,以便服务器API不被再次调用,直到第一个完成。 这是我的疑惑。 1)首先,当我收到来自服务器的响应,当数据更多时,需要更新经历for循环,我的用户界面变得没有反应。 所以为了改进,我需要在一个调度队列中运行for循环代码。 或者,他们有更好的方法来做到这一点吗? 另外,更新完成后,当应用程序进入后台时,我也需要调度队列,当应用程序进入后台? 2)其次,是他们使用activeSyncCounter的任何替代

NSOperationQueue似乎performance缓慢

我是NSOperationQueue的新手,但是在当前的项目中,我需要一种取消asynchronous操作的方法。 我原来的代码使用GCD和工作。 这里是: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),^{ rg = [[RiverGauge alloc] initWithStationInfo:[station id] forHistoryInHours:48 inThisFormat:nil]; dispatch_async(dispatch_get_main_queue(), ^{ [hud removeFromSuperview]; UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; GaugeViewController *vc = [sb instantiateViewControllerWithIdentifier:@"GaugeDetailViewController"]; [vc setRiverGauge:rg]; [self.navigationController pushViewController:vc animated:YES]; }); }); RiverGauge是一个可能需要很长时间的networking操作,特别是当用户在农村时。 我见过的大多数post都build议将其移入NSOperationQueue。 我做了如下: NSOperationQueue *queue = [NSOperationQueue new]; NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getGaugeDetail:) object:[station id]]; [queue addOperation:op]; 在getGaugeDetail方法(在select器参数)中,我有以下代码: […]

使用GCD完成块

我正在开发一个需要完成块和“障碍”function的应用程序。 据我所知, NSOperation API有一个completionBlock属性,GCD API有dispatch_barrier_(a)sync函数来处理屏障需求。 为了完成这两个需求,我该怎么做? 谢谢!

如何找出是什么导致IOS设备上的错误崩溃报告?

基督复活(复活节快乐)! 在小应用程序重构之后testing应用程序时,我收到了“Crashlytics”崩溃日志。 正在尝试更改模型以避免双重同步方法调用(同时)通过将所有下一个调用(从第二个调用开始)发送到专用串行队列。 我已经添加了下面的代码: @interface SynchronizationModel () @property (nonatomic) dispatch_queue_t synchronizationQueue; @end @implementation SynchronizationModel BOOL isSynchronizationNotCalled = YES; #pragma mark – Synchronization + (dispatch_queue_t)synchronizationQueue { static dispatch_queue_t queue; static dispatch_once_t onceToken; const char *queueID = "com.app.synchronizationModel.queue"; dispatch_once(&onceToken, ^{ queue = dispatch_queue_create(queueID, DISPATCH_QUEUE_SERIAL); }); return queue; } + (void)sychronizeOfflineObjects { if (isSynchronizationNotCalled) { NSLog(@"%d synchronization without […]

上传大背景线程中的图像。

我需要上载一个图像到web服务。 以下是我已经返回上传图片的代码片段。 图像尺寸非常大(约6Mb)。 我使用GCD在后台线程中上传图像。 if([VSCore ConnectedToInternet ]) { bgTask = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler: ^{ dispatch_async(dispatch_get_main_queue(), ^{ //[application endBackgroundTask:self->bgTask]; //self->bgTask = UIBackgroundTaskInvalid; }); }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [vrs write:data toURI:URI]; [[UIApplication sharedApplication]endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); // } -(BOOL)write:(NSData *)data toURI:(NSString *)URI { BOOL retVal = NO; NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%d", [data length]]; NSRange range […]

带有嵌套Parse查询的GCD

func getPosts(skip: Int){ var query = PFQuery(className: self.parseClassName!) query.includeKey("posted_by") query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in if error == nil && objects != nil { if let objects = objects as? [PFObject] { var requestGroup = dispatch_group_create() for post in objects { dispatch_group_enter(requestGroup) let queryKommentar1 = PFQuery(className:"Comment") queryKommentar1.whereKey("posted_to", equalTo: post) queryKommentar1.limit = 3 […]

使用Geocoder更新属性来获取用户实际地址EXC_BAD_ACCESS

我相对是一个新的iOS开发人员,即时通讯处理地理编码器完成处理程序 ,我不明白如何更新地址parsing器的地址find一个variables,并做一些与它的东西。 总之, 我希望geocoder块在某些方法中完成使用信息 。 – (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"didUpdateToLocation: %@", newLocation); CLLocation *currentLocation = newLocation; if (currentLocation != nil) { longitudeLabel = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; latitudeLabel = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; } [locationManager stopUpdatingLocation]; // Reverse Geocoding NSLog(@"Resolving the Address"); __block NSString * **annotation**; [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { […]

使用Grand Central Disatch从文件加载UIImage

我正在尝试使用gcd在后台加载图像。 我的第一次尝试没有奏效: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0), ^{ dispatch_async(dispatch_get_main_queue(), ^{ // create UI Image, add to subview }); }); 注释掉后台队列代码,只留下主队列调度块不起作用: dispatch_async(dispatch_get_main_queue(), ^{ // create UI Image, add to subview }); 在gcd块里面做ui的东西有一些窍门吗? 为了回应mattjgalloway的评论,我只是试图在后台线程中加载一个大的图像。 以下是我最初尝试的完整代码: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0), ^{ UIImage* img = [[UIImage alloc] initWithContentsOfFile: path]; dispatch_async(dispatch_get_main_queue(), ^{ imageView = [[[UIImageView alloc] initWithImage: img] autorelease]; imageView.contentMode = UIViewContentModeScaleAspectFill; CGRect photoFrame = self.frame; imageView.frame […]