Tag: objective c blocks

如何判断循环中的块是否都已经完成执行?

我有一个循环设置,下载一系列的图像,我将稍后使用animation使用UIImageView的animationImages属性。 我想知道什么时候我的循环里面的所有块已经完成执行,所以我可以开始animation,并想知道如何能够告诉他们什么时候完成? 谢谢! for (PFObject *pictureObject in objects){ PFFile *imageFile = [pictureObject objectForKey:@"image"]; NSURL *imageFileURL = [[NSURL alloc] initWithString:imageFile.url]; NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageFileURL]; [tokenImageView setImageWithURLRequest:imageRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { [self.downloadedUIImages addObject:image]; //This is a mutableArray that will later be set to an UIImageView's animnationImages } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError […]

我为什么要selectGCD而不是NSOperation,并为高级应用程序块?

苹果的Grand Central Dispatch参考说: “…如果您的应用程序需要在系统的Unix级别上运行 – 例如,如果需要操作文件描述符,Mach端口,信号或定时器,GCD不限于系统级应用程序,而是在您之前将它用于更高级别的应用程序,您应该考虑Cocoa中提供的类似function(通过NSOperation和块对象)是否更容易使用或更适合您的需求。“ http://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html 我实际上不能想到高级应用的情况,在这种情况下,GCD的使用是强制性的,NSOperation可以/不应该使用。 有什么想法吗?

完成块的返回结果

所以我试图在Twitter API(为其他项目)之上构build一个图层,并且需要find一种方法将Twitter操作的结果返回到抽象层。 现在我的设置是这样的,例如: -(NSDictionary *)sendTweet:(Tweet *)tweet { __block NSMutableDictionary *responseDictionary; NSLog(@"Sending tweet"); NSMutableDictionary *twitterRequestDictionary = [[NSMutableDictionary alloc] init]; [twitterRequestDictionary setObject:tweet.tweetBody forKey:@"status"]; TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1/statuses/update.json"] parameters:twitterRequestDictionary requestMethod:TWRequestMethodPOST]; [request setAccount:self.userAccount]; [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil]; NSLog(@"Response dictionary: %@", responseDictionary); return responseDictionary; }]; } 但是因为'performRequestWithHandler:'方法返回'void',所以最后一行会导致错误。 […]

块完成后,如何访问__blockvariables?

我正在做一些与Parse.com后台操作,但这是一个关于__blockvariables的一般问题。 我想定义一个variables,用一个完成块运行后台networking操作,可能修改块内的variables,然后在块外部访问它。 但总是零。 如何保留块外的variables? 这是一个类方法,所以使用实例variables不是一个选项。 __block PFObject *myObject = nil; PFQuery *query = [PFQuery queryWithClassName:@"ClassName"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (objects.count) myObject = [objects lastObject]; }]; NSLog(@"%@",myObject);

为什么不从NSNotificationCenter删除观察者:addObserverForName:usingBlock被调用

我很困惑,为什么在下面的代码中永远不会删除观察者。 在我看来,我有以下几点: -(void)viewDidAppear:(BOOL)animated{ id gpsObserver = [[NSNotificationCenter defaultCenter] addObserverForName:FI_NOTES[kNotificationsGPSUpdated] object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){ NSLog(@"run once, and only once!"); [[NSNotificationCenter defaultCenter] removeObserver:gpsObserver]; }]; } 观察者永远不会被移除,并且每次发送通知时都会输出语句。 任何人可以提供任何指导?

如何编写Objective-C完成块

我在需要从我的视图控制器调用类方法的情况下,让它做的事情,但只有在类方法完成后才执行一些操作。 (我想我需要的是一个完成块,但如果我错了,请纠正我。) 情况如下: 我使用Parse.com为我的应用程序后端。 当用户注册一个帐户时,他们在popup框中input他们的姓名,公司和其他信息,然后点击提交。 提交button链接到一个类方法(如下所示),它接收PFUser对象和公司名称并创build一些数据库对象。 函数完成之后,popup窗口将使用委托来解除。 问题是我需要创build这些对象以特定的顺序发生,因为它们依赖eachothers objectId的存在。 问题是,点击提交之后立即调用closurespopup窗口的委托方法,因为它是堆栈中的下一个。 当保存一个Parse对象时,会调用一个类似下面的方法:(这是我希望写的东西,我认为可以解决我的问题) [someParseObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { // Code here runs AFTER the method completes. // This also happens on another thread which // i'd like to implement as well. }]; 所以,我需要弄清楚如何做下面的事情:(一切都与块是完全错误的,我敢肯定) SignUpViewController.m myUserOrg *userOrg = [myUserOrg object]; // myUserOrg = Custom PFObject Subclass // […]

用块创build代表

我喜欢积木,当我不能使用它时,这让我很难过。 尤其是,每当我使用委托时,都会发生这种情况(例如:使用UIKit类,主要是预块function)。 所以我想知道…有没有可能 – 使用ObjC的疯狂的力量,做这样的事情? // id _delegate; // Most likely declared as class variable or it will be released _delegate = [DelegateFactory delegateOfProtocol:@protocol(SomeProtocol)]; _delegate performBlock:^{ // Do something } onSelector:@selector(someProtocolMethod)]; // would execute the given block when the given selector is called on the dynamic delegate object. theObject.delegate = (id<SomeProtocol>)_delegate; // Profit! performBlock:onSelector: 如果YES […]

实现一个块作为callback使用的方法

我想写一个类似这样的方法: +(void)myMethodWithView:(UIView *)exampleView completion:(void (^)(BOOL finished))completion; 我已经基本上从苹果的UIView的类方法剥离的语法: + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion; 并期望它被使用像这样: [myFoo myMethodWithView:self.view completion:^(BOOL finished){ NSLog(@"call back success"); }]; 我的问题是我怎样才能实现呢? 如果有人能指出我的正确的文档,那将是非常好的,一个非常基本的例子将不胜感激(或堆栈溢出类似的答案 – 我找不到一个)。 对代表们来说,我还不是很了解,以确定这是否是正确的做法! 我已经把我期望它在实现文件中的一个粗略的例子,但是因为我不能find信息这是猜测工作。 + (void)myMethod:(UIView *)exampleView completion:(void (^)(BOOL finished))completion { // do stuff if (completion) { // what sort of syntax goes here? If I've constructed this […]