Tag: objective c block

块的执行总是延迟

我对IOS很新。 我想在viewDidLoad获取设备上的所有图片。 但是在代码中调用NSLog(@"%d", photos.count)后,块总是执行如下。 如何处理这种情况? __block NSMutableArray* photos = [[[NSMutableArray alloc] init] retain]; ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; void (^assertsEnumerator)(ALAsset *, NSUInteger , BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) { if(result) { NSLog(@"Assert name : %@", [result valueForProperty:ALAssetPropertyURLs]); [photos addObject:[UIImage imageWithCGImage:[result aspectRatioThumbnail]]]; } }; void (^groupEnumerator)(ALAssetsGroup*, BOOL*) = ^(ALAssetsGroup *group, BOOL […]

嵌套块和引用自我

我有一个块,我使用self所以我宣布对自我的一个弱引用: __weak MyClass *weakSelf = self; 现在我的问题: 我在定义weakSelf遇到一个错误,我不明白这应该是什么意思。 无法在自动variables上指定弱属性 在我的街区里面,我将weakSelf传递给另一个街区,我不知道现在是否必须再次做同样的事情: __weak MyClass *weakWeakSelf = weakSelf; 然后把weakWeakSelf传给那个块?

目标C中符号^的含义

可能重复: 目标C中的插入符号 Objective-C中这个^语法是什么意思? 在Objective C中search符号^的含义让我感到厌倦,尤其是在后台运行的任务中,我已经看到了很多项目。 我会把一个链接http://developer.apple.com/library/ios/#samplecode/StitchedStreamPlayer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010092和MyStreamingMovieViewController.m你可以find以下内- (IBAction)endScrubbing:(id)sender method 。 timeObserver = [[player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(tolerance, NSEC_PER_SEC) queue:dispatch_get_main_queue() usingBlock: ^(CMTime time) { [self syncScrubber]; }] retain]; } 另外http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html – (void)applicationDidEnterBackground:(UIApplication *)application { UIApplication* app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ // Clean up any unfinished task business by marking where you. // stopped or ending the task […]

OCMock和块testing,执行

以下是testing中的方法: – (void)loginWithUser:(NSString *)userName andPass:(NSString *)pass { NSDictionary *userPassD = @{@"user":userName, @"pass":pass}; [_loginCntrl loginWithUserPass:userPassD withSuccess:^(NSString *authToken){ // save authToken to credential store } failure:^(NSString *errorMessage) { // alert user pass was wrong }]; } 我想testing的是,在成功块中的其他依赖/ OCMockObject _credStore被调用适当的方法。 所以目前loginCtrl和credStore的依赖关系是OCMockObjects,我可以对这些存根/期望。 我将存根loginController以某种方式执行该块时调用? 我已经看了一些与OCMock有关的残块的问题,我不能把我的头围绕在他们正在做的事情上,是否适合这种情况。 实际上,我想要做的就是OCMock来启动块([成功调用] ??),以便代码_credStore saveUserPass完成并可以在_credStore上进行validation。 我停下来的地方: – (void)test_loginWithuserPass_succeeds_should_call_credStore_setAuthToken { NSDictionary *userPassD = @{@"user":@"mark", @"pass":@"test"}; id successBlock = […]

multithreading:只有在完成执行其他方法后才执行方法调用

我试图按照要求asynchronous处理方法,一旦第一个方法完成,只有第二个方法应该开始执行。 问题是第一个方法本身具有在后台线程上运行的代码。 我试过dispatch_semaphore_wait,但也没有工作。 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, queue, ^{ [self firstMethod]; NSLog(@"firstMethod Done"); }); dispatch_group_notify(group, queue, ^ { NSLog(@"1st method completed"); NSLog(@"2nd method starting"); [self secondMethod]; }); FirstMethod本身就像这样在另一个工作线程上运行 -(void)firstMethod { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ //processing here….. }]; 什么是最好的方法来实现它,我不能改变一些第三方提供的firstMethod的定义,也改变它意味着改变这个方法被调用的地方现有的代码很多

完成处理程序在iOS上如何工作?

我试图了解完成处理程序和块。 我相信你可以使用块进行许多深层次的编程,而不需要完成处理程序,但是我想我明白,完成处理程序是基于块的。 (所以基本上完​​成处理程序需要块,而不是相反方向)。 所以我在互联网上看到了关于旧的twitter框架的代码: [twitterFeed performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { if (!error) { self.successLabel.text = @"Tweeted Successfully"; [self playTweetSound]; } else { // Show alert } // Stop indicator sharedApplication.networkActivityIndicatorVisible = NO; }]; 这里我们调用了一个方法,它执行TWRequest,并在返回responseData&urlResponse&error时返回。 只有当它返回时,才会执行被testing的块并停止活动指示器。 完善! 现在,这是一个设置,我有一个不同的应用程序的工作,但我试图把碎片放在一起: @interface Define an ivar typedef void (^Handler)(NSArray *users); Declare the method +(void)fetchUsersWithCompletionHandler:(Handler)handler; @implementation +(void)fetchUsersWithCompletionHandler:(Handler)handler { //…Code […]

如何将活动指标添加到UITableView中的图像?

我创build了自定义tableView并使用其他类中的自定义tableView类来显示tableView .. 我从服务器加载图像,并显示在tableViewRow ..每个image是不同的.. 在屏幕上,只有7个图像会出现,当我向下滚动时,前3个图像重复一段时间,当这些图像加载显示正确的图像..但最初直到新的图像会显示旧图像,我想要放置一个活动指示器来显示图像正在加载而不是旧图像 我想在image的位置添加activity Indicator ,直到image加载.. 我的代码是… self.tableViewX = tableView; static NSString *simpleTableIdentifier = @"SimpleTableCell"; SimpleTableCell *cell = (SimpleTableCell *)[tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ { NSString *imageURL = [NSString stringWithFormat: @"www.xyz.image.png"]; cell.thumbnailImageView.image = [UIImage imageWithData:[NSData […]

不能从块添加对象到NSMutableArray

我有一种感觉,那就是我的问题真的是阻塞,但也许是另一回事。 我正在尝试将地址转换为地址,并将坐标放置到数组中以便稍后使用。 当我尝试调用我尝试添加到块中数组的对象之一时,会在底部产生exception。 在任何NSLogs在块文本内打印出来之前,exception也会被触发。 什么是正确的方法来处理这个? 谢谢。 – (NSMutableArray *)convertAddressToGeocode:(NSString *)addressString { //return array with lat/lng __block NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:0]; CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:addressString completionHandler:^ (NSArray* placemarks, NSError* error) { for (CLPlacemark* aPlacemark in placemarks) { // Process the placemark. if (error){ NSLog(@"Geocode failed with error: %@", error); [self […]

dispatch_async复制内部块

鉴于以下(手动引用计数): void (^block)(void) = ^ { NSLog(@"wuttup"); } void (^async_block)(void) = ^ { block(); } dispatch_async(dispatch_get_main_queue(), async_block); 将“块”复制,而不是从堆叠中被摧毁?

Objective-C块,recursion失败

Sup家伙, 我试图做一个自我调用的function,但把所有的东西放在一个块上, 正如你所看到的,下面的函数被称为无限次(直到arcrandom返回一个低于50的数),你应该期望输出一个可变数量的“RUNNING”消息,这取决于机会。 void (^_test_closure)(void) = ^ { NSLog(@"RUNNING"); if(arc4random() % 100 > 50) { _test_closure(); } }; _test_closure(); 但是,运行它时,我得到一个EXC_BAD_ACCESS错误,我发现的原因是当代码尝试调用闭包内的_test_closure时,它基本上指向无处。 有谁知道如何使上面的代码工作?