objective-c-blocks

AFHTTPRequestOperationManager return data in block

你。 提交于 2019-12-02 10:18:06
I created an APIController in my application that has several methods that call specific api urls and return a model object populated with the result of the api call. The api works with json and up to now my code looks like the following: //Definition: - (MyModel *)callXYZ; - (MyModel *)callABC; - (MyModel *)call123; //Implementation of one: - (MyModel *)callXYZ { //Build url and call with [NSData dataWithContentsOfURL: url]; //Create model and assign data returned by api return model; } Now I want to use the great AFNetworking framework to get rid of that "dataWithContentsOfURL" calls. So I

Obj-C class method results from block

做~自己de王妃 提交于 2019-12-02 10:08:31
I understand that this function first return "images" then "findObjectsInBackgroundWithBlock" retrieve data that's why results is nil. 1 - how to return array from block? 2 - how to put this block not in main thread? +(NSMutableArray *)fetchAllImages{ __block NSMutableArray *images = [NSMutableArray array]; PFQuery *query = [PFQuery queryWithClassName:@"Photo"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { for (PFObject *object in objects) { PFFile *applicantResume = object[@"imageFile"]; NSData *imageData = [applicantResume getData]; NSString

Waiting for a block to finish [duplicate]

浪子不回头ぞ 提交于 2019-12-02 09:09:18
This question already has an answer here: Return value for function inside a block 3 answers I have a UITableView and I'm attempting to get the number of rows. However, I'm having trouble using blocks. In the code below I'd just like to return count, but as I now understand blocks are asynchronous. I've looked around trying to find a solution but none of them worked. One solution I tried was this: How do I wait for an asynchronously dispatched block to finish? but when I clicked on the button to go to the view with the table, it just froze when the button was clicked. I tried some others, but

retainCount in blocks show extrange behavior

不打扰是莪最后的温柔 提交于 2019-12-02 08:14:09
I got the this code in a class: - (void)cancel { if (_cancelBlock) _cancelBlock(); } - (void)overrideCancelWithBlock:(void(^)(void))cancelBlock { [_cancelBlock release]; NSLog(@"AsyncOperation-overrideCancelWithBlock-[cancelBlock retainCount]=%lu (before)", [cancelBlock retainCount]); _cancelBlock = [[cancelBlock copy] retain]; NSLog(@"AsyncOperation-overrideCancelWithBlock-[_cancelBlock retainCount]=%lu (after)", [_cancelBlock retainCount]); } - (void)dealloc { NSLog(@"AsyncOperation-dealloc-[_cancelBlock retainCount]=%lu (before)", [_cancelBlock retainCount]); [_cancelBlock release]; NSLog(@

NSTimer does not invalidate

岁酱吖の 提交于 2019-12-02 07:25:41
问题 I have a problem invalidating my timer. @property (nonatomic, strong) NSTimer *timer; Inside my block on success, I am allocating and setting my timer on main thread: dispatch_async(dispatch_get_main_queue(), ^{ self.timer = [NSTimer scheduledTimerWithTimeInterval:[Config refreshInterval].integerValue target:self selector:@selector(methodThatHasABlockCalledMentionedAbove) userInfo:nil repeats:NO]; }); At my textFieldDidBeginEditing I am invalidating my timer like this: dispatch_async(dispatch

Obj-C: __block variable not retaining data

醉酒当歌 提交于 2019-12-02 07:19:44
I think I might have an async problem going on here, which bites cause I thought I had solved it. Anyway, I am making a bunch of web service calls like so: //get the client data __block NSArray* arrClientPAs; [dataManager getJSONData:strWebService withBlock:^(id results, NSError* error) { if (error) { UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Getting Client Data Error!" message:error.description delegate:nil cancelButtonTitle:NSLocalizedString(@"Okay", nil) otherButtonTitles:nil, nil]; [alert show]; } else { arrClientPAs = results; } }]; and getJSONData is like so: - (void)

How to open/create UIManagedDocument synchronously?

旧巷老猫 提交于 2019-12-02 06:02:14
问题 As mentioned in title, I would like to open UIManagedDocument synchronously, i.e, I would like my execution to wait till open completes. I'm opening document on mainThread only. Current API to open uses block [UIManagedDocument openWithCompletionHandler:(void (^)(BOOL success))]; Locks usage mentioned at link works well on threads other than main thread. If I use locks on mainThread , it freezes execution of app. Any advice would be helpful. Thanks. 回答1: First, let me say that I strongly

iOS 5 blocks crash only with Release Build

邮差的信 提交于 2019-12-02 03:16:39
I have using blocks and ARC, and found in some situation, iOS only crash in Release build. It was wrong way to write code, like this. -(IBAction)clickedButtonA:(UIBarButtonItem*)sender event:(UIEvent*)event { NSMutableArray *arrRows = [NSMutableArray arrayWithCapacity:0]; #warning this code only crash on Release Build.... Don't use this NSMutableDictionary * dicRow = [NSMutableDictionary dictionaryWithCapacity:0]; [arrRows addObject:dicRow]; dispatch_block_t block = ^{ NSString *str = [NSString stringWithFormat:@"%@",[_tweet valueForKey:@"text"]]; [[UIPasteboard generalPasteboard] setString

Saving a block in instance variable

最后都变了- 提交于 2019-12-01 22:14:11
How do we declare a global(private instance variable) to accept a block in it. Do we need to synthesis it & what are the memory management implications with it. I have a block received from a third party method which I want to save in the instance variable & use it at a later stage. Here's an (ARC-less) example of storing a block for a completion callback after doing some work in the background: Worker.h: @interface Worker : NSObject { void (^completion)(void); } @property(nonatomic,copy) void (^completion)(void); - (void)workInBackground; @end Worker.m: @implementation Worker @synthesize

ARC behavior within a recursive block

被刻印的时光 ゝ 提交于 2019-12-01 18:02:16
问题 I've made these two utility funcions: + (void)dispatch:(void (^)())f afterDelay:(float)delay { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay*NSEC_PER_SEC)), dispatch_get_main_queue(), f); } + (void)dispatch:(void (^)())f withInterval:(float)delay { void (^_f)() = nil; // <-- A _f = ^{ f(); [self dispatch:_f afterDelay:delay]; // <-- B }; [self dispatch:_f afterDelay:delay]; } The idea is that you would be able to call: [self dispatch: block afterDelay: delay ]; - to get a