Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel Callback not Received + EventMonitor Cancel Calls is Received Twice each time #2759

Closed
Mahan3340 opened this Issue Mar 25, 2019 · 9 comments

Comments

Projects
None yet
3 participants
@Mahan3340
Copy link

commented Mar 25, 2019

What did you do?

created a Download request with AF.download and called cancel on the request

What did you expect to happen?

Receive Response Handler callback (to grab resumeData)
request.response ( .. )

What happened instead?

Did not receive callback (although progress & success is being received) so that the handler is fine and not lost and Cancel callback is received twice on EventMonitor Side, I checked that on DownloadRequest, eventMonitor?.requestDidCancel(self) is being called only once.

Alamofire Environment

**Alamofire version:5
**Xcode version:10 (latest)
**Swift version:4.2
**Platform(s) running Alamofire:iOS 12.1
**macOS version running Xcode:10.14.3

@Mahan3340 Mahan3340 changed the title Cancel Callback not Received + EventMonitor Cancel Calls Twice each time Cancel Callback not Received + EventMonitor Cancel Calls is Received Twice each time Mar 25, 2019

@jshier

This comment has been minimized.

Copy link
Contributor

commented Mar 26, 2019

I'm not sure what you mean here. Can you post your actual code?

@jshier jshier self-assigned this Mar 26, 2019

@Mahan3340

This comment has been minimized.

Copy link
Author

commented Mar 27, 2019

EventMonitor SubClass

import Foundation
import Alamofire

public class MeventMonitor: EventMonitor
{
    public func request(_ request: Request, didFailTask task: URLSessionTask, earlyWithError error: Error) {
      API.events.trigger(eventName: request.id.uuidString, information: HTTPEvent(data:request,event:HttpEventEnum.failed,error:error))
    }
    public func requestDidFinish(_ request: Request) {
      API.events.trigger(eventName: request.id.uuidString, information: HTTPEvent(data:request,event:HttpEventEnum.finished,error:nil))
    }
    public func requestDidSuspend(_ request: Request) {
       API.events.trigger(eventName: request.id.uuidString, information: HTTPEvent(data:request,event:HttpEventEnum.suspended,error:nil))
    }
    public func requestDidCancel(_ request: Request) {
       // when request.cancel() is called, it will be received here twice
        API.events.trigger(eventName: request.id.uuidString, information: HTTPEvent(data:request,event:HttpEventEnum.cancelled,error:nil))
    }
    public func requestIsRetrying(_ request: Request) {
        API.events.trigger(eventName: request.id.uuidString, information: HTTPEvent(data:request,event:HttpEventEnum.retrying,error:nil))
    }
    public func request(_ request: Request, didCompleteTask task: URLSessionTask, with error: Error?) {
        API.events.trigger(eventName: request.id.uuidString, information: HTTPEvent(data:request,event:HttpEventEnum.completed,error:error))
    }
}

DownloadManagerClass :

  public static var session = Session(interceptor:DownloadManager.interceptor,eventMonitors: [MeventMonitor()])
   public static var DOWNLOAD_MAX_RETRIES:UInt = 3
   public static var interceptor = Interceptor(
       retriers: [RetryPolicy(retryLimit:DownloadManager.DOWNLOAD_MAX_RETRIES)]
   )

public static func addDownload(downloadItem:DownloadItem)->DownloadRequest
   {
       let destination: DownloadRequest.Destination = { _, _ in
           
           return (URL(fileURLWithPath:downloadItem.filePath), [.removePreviousFile, .createIntermediateDirectories])
       }
       return DownloadManager.session.download(downloaditem.downloadUrl,method:.get, encoding:URLEncoding.default,to: destination)
   }

and then I use these like below :

self.request = DownloadManager.addDownload(downloadItem: DownloadItem(filePath: path, downloadPath: DownloadManager.getPublicOf(partial: urlString, thumb: nil)))

request.downloadProgress(closure: {
            progress in
            self.infoProgress.setProgress(value: CGFloat(progress.fractionCompleted))
        })
        request.response(completionHandler: {
            response in
            // nothing received here when request.cancel() is called, shouldn't it ?
           // if not, what is the way to catch resumeData other than eventMonitor ?
            self.handleResponse(response: response)
        })
@jshier

This comment has been minimized.

Copy link
Contributor

commented Apr 11, 2019

@Mahan3340 Can you give the bugfix/state-transitions branch a try? It should fix this issue.

@Mahan3340

This comment has been minimized.

Copy link
Author

commented Apr 12, 2019

@jshier I checked the branch and can confirm that request.response is called on request cancellation, but the eventMonitor's requestDidCancel is still called twice, I checked that when the eventMonitor is about to performEvent, there are two monitors available in the array and I guess one might be the default monitor or the request monitor itself that is performing an extra event along with the one that I have created through Session

@cnoon cnoon closed this in #2796 Apr 12, 2019

@jshier jshier added this to the 5.0.0-beta.5 milestone Apr 13, 2019

@jshier

This comment has been minimized.

Copy link
Contributor

commented Apr 13, 2019

@Mahan3340 Alamofire always has an instance of AlamofireNotifications as an EventMonitor, in addition to any EventMonitors you add, and calls all of them for every event. As long as your particular monitor isn't seeing duplicate events any more, this issue should be resolved.

@Mahan3340

This comment has been minimized.

Copy link
Author

commented Apr 13, 2019

@jshier yes but the problem is that I receive the second event in my particular monitor (MEventMonitor above)

@jshier jshier reopened this Apr 14, 2019

@jshier

This comment has been minimized.

Copy link
Contributor

commented Apr 14, 2019

@Mahan3340 Looks like there were additional issues with DownloadRequest specifically that weren't addressed by the earlier PR. If you can give the bugfix/event-consistency branch a try, I believe your issues should be fixed for real now.

@Mahan3340

This comment has been minimized.

Copy link
Author

commented Apr 14, 2019

@jshier I checked branch bugfix/event-consistency and the bug is resolved 👍 and I'm not sure if the changes included in this branch is related or not but the issue mentioned in #2782 is also not happening anymore.

@cnoon cnoon modified the milestones: 5.0.0-beta.5, 5.0.0-beta.6 Apr 15, 2019

@cnoon

This comment has been minimized.

Copy link
Member

commented Apr 23, 2019

I'm going to close this issue out since we believe it has been resolved. Please feel free to comment back if the issue persists in the beta 6 release.

@cnoon cnoon closed this Apr 23, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.