objective-c-blocks

Waiting for a block to finish [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-02 18:22:39
问题 This question already has answers here : Return value for function inside a block (3 answers) Closed 5 years ago . 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

What will happen if I have nested dispatch_async calls?

淺唱寂寞╮ 提交于 2019-12-02 18:06:41
It may be a dumb question but I need to ask and clear this up for myself. To submit a block onto a queue for execution, use the functions dispatch_sync and dispatch_async . They both take a queue and a block as parameters. dispatch_async returns immediately, running the block asynchronously, while dispatch_sync blocks execution until the provided block returns. Here are some situations: Situation 1 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul); dispatch_async(queue, ^{ [self goDoSomethingLongAndInvolved]; dispatch_async(queue, ^{ NSLog(@"this is

Correct management of addObserverForName:object:queue:usingBlock:

≡放荡痞女 提交于 2019-12-02 18:01:15
I'm still new to blocks in objective-c and wondering if I have this psuedo code correct. I'm not sure if it's enough to just remove the observer or if i have to call removeObserver:name:object: -(void) scan { Scanner *scanner = [[Scanner alloc] init]; id scanComplete = [[NSNotificationCenter defaultCenter] addObserverForName:@"ScanComplete" object:scanner queue:nil usingBlock:^(NSNotification *notification){ /* do something */ [[NSNotificationCenter defaultCenter] removeObserver:scanComplete]; [scanner release]; }]; [scanner startScan]; } Update: I'm receiving intermittent EXC_BAD_ACCESS from

How does typedef-ing a block works

谁说我不能喝 提交于 2019-12-02 17:24:49
In C/Obj-C, we do a typedef like this typedef int MYINT; which is clear. Doing typedef for a block - typedef void (^MyBlock) (int a); Now, we can use MyBlock . Shouldn't it be like - typedef void (^MyBlock) (int a) MyBlock; similar to #define ? How the syntax works? Martin R See Declaring a Block Reference in "Blocks Programming Topics": Block variables hold references to blocks. You declare them using syntax similar to that you use to declare a pointer to a function, except that you use ^ instead of *. So typedef void (^myBlock) (int a); defines a the type of a block using the same syntax as

Variable returning null after block execution

北慕城南 提交于 2019-12-02 16:13:30
问题 I am dispatching a queue to download some flickr photos on a separate thread (in viewWillAppear). When I log the contents of the array inside the block, it shows everything perfectly: dispatch_queue_t photoDowonload=dispatch_queue_create("photoDownload", NULL); dispatch_async(photoDowonload, ^{ NSArray *photoList=[FlickrFetcher topPlaces]; //downloads flickr data self.listOfCities=photoList; NSLog(@"inside block: %@", self.listOfCities); //shows contents }); but when I try to log the array

dispatch_sync vs. dispatch_async on main queue

随声附和 提交于 2019-12-02 14:06:30
Bear with me, this is going to take some explaining. I have a function that looks like the one below. Context: "aProject" is a Core Data entity named LPProject with an array named 'memberFiles' that contains instances of another Core Data entity called LPFile. Each LPFile represents a file on disk and what we want to do is open each of those files and parse its text, looking for @import statements that point to OTHER files. If we find @import statements, we want to locate the file they point to and then 'link' that file to this one by adding a relationship to the core data entity that

iOS 4 blocks and retain counts

喜你入骨 提交于 2019-12-02 13:59:49
I'm just getting started with blocks and Grand Central Dispatch. I've been told (and read in the Apple Documentation ) that any object referenced from within a block gets retained. For instance: ^{ self.layer.transform = CATransform3DScale(CATransform3DMakeTranslation(0, 0, 0), 1, 1, 1); self.layer.opacity = 1; } "self" gets retained so it leaks. To avoid that, I need to assign self to: __block Object *blockSelf = self; and then use blockSelf instead of self inside my block. My question is: what happens when your block has a lot more code and references several objects? Do I need to assign

how to add an extra argument to a block

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 11:49:59
Mailcore has a cool method that downloads an attachment and accepts a block as a parameter to return download progress: - (CTCoreAttachment *)fetchFullAttachmentWithProgress:(CTProgressBlock)block; where CTProgressBlock is defined as typedef void (^CTProgressBlock)(size_t curr, size_t max); so typically I would use it like so: //AttachmentDownloader.m int fileNum = x; // explained later CTCoreAttachment* fullAttachment = [attachment fetchFullAttachmentWithProgress:^(size_t curr, size_t max) { NSLog(@"::: this is curr: %zu", curr); NSLog(@"::: this is max: %zu\n\n", max); }]; the problem is

How to add activity indicator to the image in UITableView?

妖精的绣舞 提交于 2019-12-02 11:18:56
I have created the Custom tableView and using custom tableView class in Other class to Show the tableView .. I am Loading the image from Server and displaying it in the tableViewRow .. each image is different in it.. On Screen only 7 out of 10 images will come, and when I scroll down the first 3 images are repeating for some time and when those images get load its showing proper images.. but initially till new images will come its showing old images, I want to put a activity indicator to show that the images are loading instead of old images.. I want to add activity Indicator in the place of

Return value from inside block (Objective-C)

烈酒焚心 提交于 2019-12-02 10:18:30
I've been trying to get a value from inside a block for a few hours now, I can't understand how to use the handlers on completion and literally everything. Here's my code: + (void)downloadUserID:(void(^)(NSString *result))handler { //Now redirect to assignments page __block NSMutableString *returnString = [[NSMutableString alloc] init]; //'__block' so that it has a direct connection to both scopes, in the method AND in the block NSURL *homeURL = [NSURL URLWithString:@"https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/PortalMainPage"]; NSMutableURLRequest *requestHome = [