I was testing with it the other day and it was working. Today, after calling this method, it never calls the completion block.
Is there a case where the completion block is not called? I remember Apple said that it's always called regardless of what happened.
Here's the code block:
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
// Get ubiquitous url
NSURL *ubiq = [[NSFileManager defaultManager]
URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:
#"Documents"] URLByAppendingPathComponent:fileName];
// Create new CloudFile
CloudFile *file = [[CloudFile alloc]initWithFileURL:ubiquitousPackage];
file.data = data;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(onDocumentStateChanged:) name:UIDocumentStateChangedNotification object:file];
dispatch_async (dispatch_get_main_queue (), ^(void) {
[file saveToURL:file.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {
NSLog(#"test");
if (success) {
[self sendEvent:ICLOUD_FILE_SAVE_SUCCESSFUL object:file];
} else {
[self sendEvent:ICLOUD_FILE_SAVE_FAILED];
NSLog(#"kaiCloud: Saving failed. (%#)",fileName);
}
}];
});
});
EDIT: Note that I added NSLog(#"test") and I'm not seeing it getting logged.
I encountered the same problem (which is how I found your question).
The reason is simple. The test code is not waiting for the completion handler to be called and certainly not for it to finish executing.
Your test may work sometimes if they are long enough or happen to get stalled. But that's not reliable.
Use the WAIT macros provided and explained here and problem solved.
https://github.com/hfossli/AGAsyncTestHelper
Related
I have a method which calls an API Internally. That method does not have any completion handler.
-(void) methodToBeTested{
[self callAPIWithCompletionHandler:^(NSArray *data,NSError *error)
{
//Here I get the response and sets the models.
}];
}
Now I need to test the method "methodToBeTested" basis of the models set after the API call.
Any Suggestions?
Example:
XCTestExpectation *documentOpenExpectation = [self expectationWithDescription:#"document open"];
NSURL *URL = [[NSBundle bundleForClass:[self class]]
URLForResource:#"TestDocument" withExtension:#"mydoc"];
UIDocument *doc = [[UIDocument alloc] initWithFileURL:URL];
[doc openWithCompletionHandler:^(BOOL success) {
XCTAssert(success);
[documentOpenExpectation fulfill];
}];
[self waitForExpectationsWithTimeout:1 handler:^(NSError *error) {
[doc closeWithCompletionHandler:nil];
}];
Look at the Writing Tests of Asynchronous Operations under the Testing with Xcode documentation. https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/04-writing_tests.html
I have situation where I am iterating my array objects using enumerateObjectsUsingBlock, and I need to wait for a completion in the iteration then it should execute further code, how can I achieve this, or if any alternate I should use, below is my code
[arrPendingQueue enumerateObjectsUsingBlock:^(PendingQueues *obj, NSUInteger idx, BOOL *stop) {
//Fetch static google map based on coordinates
[self generateMapImage:obj.postObj completion:^(UIImage *image) {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:#"camera"];
NSString *resultPath = [path stringByAppendingPathComponent:[NSString stringWithFormat:#"map_%f.jpg",[[Constants getSystemDateInLocalTimeZone] timeIntervalSince1970]]];
[[NSFileManager defaultManager] createFileAtPath:resultPath contents:UIImageJPEGRepresentation(image, 1.0) attributes:nil];
postObj.imagepath = [#"camera" stringByAppendingPathComponent:[resultPath lastPathComponent]];
[DataManager saveObject:postObj];
}];
//After the completion I want to upload the data here to server
});
Basically, I am trying to download static google map from coordinate in function generateMapImage and I want the loop to wait till completion called..
For that I tried to use dispatch_semaphore_create like
[arrPendingQueue enumerateObjectsUsingBlock:^(PendingQueues *obj, NSUInteger idx, BOOL *stop) {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[self generateMapImage:obj.postObj completion:^(UIImage *image) {
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
});
But it doesn't work it doesn't wait, and executes the code before completion completed.
How to solve, please help.
Thanks.
To queue up execution of a series of a blocks, you can simply start the new block in the completion handler of the old block:
NSEnumerator *objectEnum = [arrPendingQueue objectEnumerator];
__block void (^handler) ();
handler = ^(UIImage *image)
{
// Handle image
object = [objectEnum nextObject];
if( object == nil )
{
// completion of all operations
}
else
{
// Start next operation
[self generateMapImage:object.postObj completion:handler];
}
};
[self generateMapImage:obj.postObj completion:handler];
}
A more classical or elegant way would be to use a Y-combinator. But it is not necessary. Just keep in mind, that local scope variables are cleaned up when losing their scope. handler is not retained. So maybe you have to put it into an ivar.
However, I see -generateMapImage:completion is written by you. I cannot see the content, but if you do a async call there and you can set the queue used for the call, simply set it to a serial queue. In such a case your requests will be executed serial automatically.
I currently have a share extension set up that will upload an image selected from the Photo app to a server. This works fine using the code below.
int fileNum=10;
NSItemProvider *attachment = inputItem.attachments[0];
if ([attachment hasItemConformingToTypeIdentifier:(NSString*)kUTTypeImage])
{
[attachment loadItemForTypeIdentifier:(NSString*)kUTTypeImage options:nil completionHandler:^(id item,NSError *error)
{
if (item)
{
NSLog (#"image %#",item);
//upload image here
NSData *data=[NSData dataWithContentsOfURL:item];
activityRecord.activityType=#"Images";
AppRecord *appRecord=[[AppRecord alloc] init];
appRecord.fileName=[NSString stringWithFormat:#"activity_%#%i(%i).jpg",activityRecord.supplierID,activityRecord.activityID,fileNum];
appRecord.fileBytes=data;
[fileRecords addObject:appRecord];
activityRecord.activityFiles=fileRecords;
[[Settings getInstance] uploadActivityRecord:activityRecord withDelegate:self];
[self.extensionContext completeRequestReturningItems:#[] completionHandler:nil];
}
}];
}
I had a previous problem where the loadItemForTypeIdentifier method wasn't being called, and it was resolved by calling completeRequestReturningItems within the completion block.
The problem I have now is that if I want to upload multiple files then I need to call loadItemForTypeIdentifier within a for loop (for each image) but how can I do that if the completeRequestReturningItems method will be called after the first image/item?
Many Thanks
Paul
I ran into the same problem recently and was able to resolve it by adding a counter and counting down as the images successfully completed their block. Within the loadItemForTypeIdentifier completion block I then check to see if all items have been called before calling the completeRequestReturningItems within a dispatch_once block (just for safety's sake).
__block NSInteger imageCount;
static dispatch_once_t oncePredicate;
NSItemProvider *attachment = inputItem.attachments[0];
if ([attachment hasItemConformingToTypeIdentifier:(NSString*)kUTTypeImage])
{
[attachment loadItemForTypeIdentifier:(NSString*)kUTTypeImage options:nil completionHandler:^(NSData *item ,NSError *error)
{
if (item)
{
// do whatever you need to
imageCount --;
if(imageCount == 0){
dispatch_once(&oncePredicate, ^{
[self.extensionContext completeRequestReturningItems:#[] completionHandler:nil];
});
}
}
}];
}
I can't say I feel like this is an overly elegant solution however, so if someone knows of a more appropriate way of handling this common use case I'd love to hear about it.
I am looking for a small scenario that how can we trace the "dispatch_async" is running or not?.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
//back ground process
});
In my case, my app will be in foreground I started the back ground thread and when I bring app from background to foreground I need to check whether it is still running or not. I should not call the same process if it is still running. any idea?
The easiest way to do this (without keeping a reference to every dispatch or a flag for entering/leaving asynchronous tasks) is by using dispatch_group notifications. See the example link and code below:
- (void)downloadPhotosWithCompletionBlock:(BatchPhotoDownloadingCompletionBlock)completionBlock
{
// 1
__block NSError *error;
dispatch_group_t downloadGroup = dispatch_group_create();
for (NSInteger i = 0; i < 3; i++) {
NSURL *url;
switch (i) {
case 0:
url = [NSURL URLWithString:kOverlyAttachedGirlfriendURLString];
break;
case 1:
url = [NSURL URLWithString:kSuccessKidURLString];
break;
case 2:
url = [NSURL URLWithString:kLotsOfFacesURLString];
break;
default:
break;
}
dispatch_group_enter(downloadGroup); // 2
Photo *photo = [[Photo alloc] initwithURL:url
withCompletionBlock:^(UIImage *image, NSError *_error) {
if (_error) {
error = _error;
}
dispatch_group_leave(downloadGroup); // 3
}];
[[PhotoManager sharedManager] addPhoto:photo];
}
dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{ // 4
if (completionBlock) {
completionBlock(error);
}
});
}
Note how:
dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{ // 4
if (completionBlock) {
completionBlock(error);
}
});
will not be called until after
dispatch_group_leave(downloadGroup); // 3
is called.
You should setup your threading to where you can work with callbacks like this to determine states. You should try to avoid using boolean flags at all costs, as this is exactly what dispatch groups are for. It's also hard to keep track of numerous asynchronous calls using boolean states.
link: dispatch groups
The question is wrong - dispatch_async is running while you call it and stops running when the call returns, which is practically immediately. What you really want to know is whether the dispatched block is running or not. The simplest way is something along the lines of
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
[self blockIsRunning:YES];
// do stuff
[self blockIsRunning:NO];
});
or if you want to know whether the block has run once, you would do something like
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
[self blockStarted];
// do stuff
[self blockFinished];
});
Alternatively, use NSOperationQueue and a subclass of NSOperation so instead of an anonymous block you have a proper object that you can ask whether it is ready, cancelled, executing, or finished.
I'm developing a library that gets json data from a server, and I'm using NSURLSessionDataTask. In order to test my library I created a new project that calls this library method.
typedef void (^CompletionBlock)();
// More stuff...
- (void)downloadAllPodcastsMetadataWithCompletionHandler:(CompletionBlock)block{
NSURL *url = [NSURL URLWithString:#"MyServerURL"];
NSURLSessionDataTask *dataTask = [self.session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// Connection and response handling stuff...
// When my data is saved, executes the block parameter
dispatch_async(dispatch_get_main_queue(), ^{
block();
});
}
[dataTask resume];
}
EDIT: I forgot to put [dataTask resume] here, but it's in my code. Sorry.
EDIT 2: So, as some of you have said, i changed dispatch_sync with dispatch_async. But the result is the same :(
In my test project i call this method like this.
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[manager downloadAllPodcastsMetadataWithCompletionHandler:^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}];
But the network activity Indicator never shows off. It's like the block parameter never executes.
It's because it executes the NSURLSessionDataTask logic inside a library and then I should use something else instead dispatch_sync?
I already checked NSURLSessionDataTask not executing the completion handler block and I think I do the same. If it helps, manager is a Singleton. Any thoughts?
Thank you.
You need to start the download with [dataTask resume];.
You need to add [dataTask resume]; and also add this in completion handler
dispatch_async(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
});
I tested some code.
Dispatch_sync won't work.
dispatch_sync(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
});
Dispatch_async worked for me. But I had to put the code on veiwWillAppear.
Not sure what's going to happen using it inside a block...
dispatch_async(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
});
Well, i'm dumb.
There was an error in my previous code i didn't notice, so the completion block war never executing. That is what happens when someone has too much confidence in his code.
Sorry for bothering you.