Skip to content
This repository has been archived by the owner on Jul 25, 2023. It is now read-only.

Get Progress without KVO #47

Closed
ghost opened this issue Oct 13, 2015 · 11 comments
Closed

Get Progress without KVO #47

ghost opened this issue Oct 13, 2015 · 11 comments
Labels

Comments

@ghost
Copy link

ghost commented Oct 13, 2015

Is there anyway to get upload progress without KVO? I am getting weird behavior when the app stays in the background too long. I remove the observer before I deallocate, but that doesn't seem to help. Closing the app and reopening fixes that issue.

@aclev
Copy link
Contributor

aclev commented Oct 13, 2015

Hi @manikkalra the only way to get progress on an upload is with the NSProgress object on the upload task. Can you provide an example of how you are attaching the observer? We can discuss other methods of obtaining progress for upload/download/async methods if you have suggestions.

@aclev aclev self-assigned this Oct 13, 2015
@aclev aclev added the question label Oct 13, 2015
@ghost
Copy link
Author

ghost commented Oct 13, 2015

This is how I am using it. The class I am using it is a shared instance. So, I needed to find a way to remove the observer without deallocating the class. Hence, I removed the observer right before I added it again (this method is called every-time I upload). Is there something I may be missing?

 ODItemContentRequest *request = [[[[self.odClient drive] special:@"approot"] itemByPath:filename] contentRequest];
self.uploadTask = [request uploadFromData:jpegData completion:^(ODItem *item, NSError *error) {

                    [[[[[self.odClient drive] items:item.id] createLinkWithType:@"view"] request] executeWithCompletion:^(ODPermission *response, NSError *error) {

                            .........
                           NSString *sharedLink = response.link.webUrl;
                            dispatch_async(dispatch_get_main_queue(), ^{
                                success(sharedLink, item.id);
                            });
                    }];
            }];

            // Set up progress
            dispatch_async(dispatch_get_main_queue(), ^{

                [self removeObserver];
                [self.uploadTask.progress addObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted)) options:0 context:ProgressObserverContext];
            });

And here is how I remove the observer:

 -(void)removeObserver
{
    @try
    {
        [self.uploadTask.progress removeObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted)) context:ProgressObserverContext];
    }
    @catch (NSException * __unused exception) {}
}

@aclev
Copy link
Contributor

aclev commented Oct 13, 2015

Are you sure that the removeOberserver is getting called and not failing? You are catching an exception and not doing anything with it.

@ghost
Copy link
Author

ghost commented Oct 13, 2015

If the observer is not yet set up, I expect it to fail. However, the removeObserver call will crash the app if not caught, and hence the empty catch statement.

@aclev
Copy link
Contributor

aclev commented Oct 13, 2015

You are calling removeOvserver before you have attached the observer. You need to remove the observer after the upload is complete.

@ghost
Copy link
Author

ghost commented Oct 13, 2015

Here's what I am doing now. I'll test it a little more (KVO stops working after the app has been in the background for a while)

ODItemContentRequest *request = [[[[self.odClient drive] special:@"approot"] itemByPath:filename] contentRequest];
self.uploadTask = [request uploadFromData:jpegData completion:^(ODItem *item, NSError *error) {

                if (error)
                {
                    dispatch_async(dispatch_get_main_queue(), ^{

                        [self removeObserver];
                        err(error);
                    });
                }
                else
                {
                    dispatch_async(dispatch_get_main_queue(), ^{

                        [self removeObserver];
                        progress(1.0, YES);
                    });

                    [[[[[self.odClient drive] items:item.id] createLinkWithType:@"view"] request] executeWithCompletion:^(ODPermission *response, NSError *error) {

                        if(error)
                        {
                            dispatch_async(dispatch_get_main_queue(), ^{
                                err(error);
                            });
                        }
                        else
                        {
                            NSString *sharedLink = response.link.webUrl;
                            dispatch_async(dispatch_get_main_queue(), ^{
                                success(sharedLink, item.id);
                            });
                        }
                    }];
                }
            }];

            // Set up progress
            dispatch_async(dispatch_get_main_queue(), ^{

                [self.uploadTask.progress addObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted)) options:0 context:ProgressObserverContext];
            });

@ghost
Copy link
Author

ghost commented Oct 13, 2015

So after some testing, the progress does not work after the app has gone into the background a few times. Any advice for me?

@aclev
Copy link
Contributor

aclev commented Oct 16, 2015

I haven't been able to reproduce your issue here, Can you try setting self.uploadTask to nil after your remove the observer?

@ghost
Copy link
Author

ghost commented Oct 16, 2015

I will try that and let you know. Thank you!

@ghost
Copy link
Author

ghost commented Oct 21, 2015

I tired that but unfortunately it doesn't help :\

@kevklam
Copy link
Contributor

kevklam commented Apr 20, 2016

Hi @manikkalra,
I am closing this issue as it has been inactive for a long time - please reopen if this needs further attention.
Thanks!

@kevklam kevklam closed this as completed Apr 20, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants