Moya: Failure code 999

Created on 23 Feb 2016  ยท  19Comments  ยท  Source: Moya/Moya

Hello, I use Mayo to wrap my network in my swift app.
I want to connect with my own API.

I first created a test a User case :

public enum UserAPI {
    case Information
}

extension UserAPI: TargetType {
    public var baseURL: NSURL { return NSURL(string: "http://127.0.0.1:3000/user")!}

    public var path: String {
        switch self {
        case .Information:
            return "/"
        }
    }

    public var method: Moya.Method {
        return .GET
    }

    public var parameters: [String: AnyObject]? {
        return nil
    }

    public var sampleData: NSData {
        switch self {
        case .Information():
            return "{}".dataUsingEncoding(NSUTF8StringEncoding)!
        }
    }

    public func constructProvider(target: UserAPI) -> Endpoint<UserAPI> {
        let token = TokenAccess.sharedInstance.token!
        let endpoint: Endpoint<UserAPI> = Endpoint<UserAPI>(URL: "\(target.baseURL)\(target.path)", sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters)
        return endpoint.endpointByAddingHTTPHeaderFields(["Authorization": "Bearer \(token)"])
    }
}

Then the call :

let provider = MoyaProvider<UserAPI>()
provider.request(UserAPI.Information) { (result) -> () in
    print("result request : \(result)")
}

And then the result :

result request : .Failure(Underlying(Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=http://127.0.0.1:3000/user/, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=http://127.0.0.1:3000/user/}))

The base URL is okay. I am making a transition, in a network abstraction, I used Alamofire previously, and it worked well. I am trying to move my code to Moya.

Thanks in advance for your help. :+1:

Most helpful comment

Are you retaining the provider anywhere? I've seen 999 errors when whatever is making the request is deallocated

All 19 comments

Are you retaining the provider anywhere? I've seen 999 errors when whatever is making the request is deallocated

If you are using rails. Try start rails with -b 0.0.0.0 like "rails s -b 0.0.0.0". Thats way it will be accessible. 999 errors is indeed confusing.

@wattson12 it was the problem. Thanks for you help !

Thank you @wattson12 !

@remirobert can you show a snippet of your solution provided by @wattson12 please?

@matteogazzato You just need to retain the provider. You can do it by e.g. setting the provider as a property for your controller. โœŒ๏ธ

@sunshinejr so simple and so common i didn't think about it...thank's!

@matteogazzato No problemo :beers:

But why retaining the Provider would solve this problem?

@PhilCai1993 because you need to keep a reference on it.

@remirobert What I don't understand is that Why should I keep a reference on it? If I don't, will the provider be released outside the scope(For example, I make the request in viewDidLoad, and don't keep a reference.)? I didn't inspect the implementation of Moya...

That's right! If you don't have it in a property, it gets deallocated as soon as it leaves the scope.

Ash Furrow
https://ashfurrow.com/

On April 10, 2016 at 10:25:58 AM, philcai ([email protected](mailto:[email protected])) wrote:

@remirobert(https://github.com/remirobert) What I don't understand is that Why should I keep a reference on it? If I don't, will the provider be released outside the scope(For example, I make the request in viewDidLoad, and don't keep a reference.)? I didn't inspect the implementation of Moya, it a little strange...

โ€”
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub(https://github.com/Moya/Moya/issues/412#issuecomment-207993002)

@ashfurrow Shouldn't this crash however? It is very hard to trace these kind of bugs.

Agreed, hard to debug. Moya tries to make it hard to crash the app ๐Ÿ˜‰

Ash Furrow
https://ashfurrow.com/

On April 15, 2016 at 11:30:35 AM, nikos kanellopoulos ([email protected](mailto:[email protected])) wrote:

@ashfurrow(https://github.com/ashfurrow) Shouldn't this crash however? It is very hard to trace these kind of bugs.

โ€”
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub(https://github.com/Moya/Moya/issues/412#issuecomment-210507282)

I ran into this issue too after following the Basic Setup and not retaining the provider. Happy to put in a PR to the docs to highlight how you need the retain (as I reckon I might not be the first ๐Ÿ˜‰ ).

ps: great library! Thanks @ashfurrow ๐Ÿ˜„

@tomj because you need to keep a reference on it.

@tomj it would be _awesome_ if you could make a PR with how would you like to be informed about retaining the provider. ๐Ÿ™Œ

@remirobert - you bet! ๐Ÿ˜„

I just wanted to point out that the Basic Setup docs don't 100% allude to the fact that you need to keep a reference to the provider, which I'm guessing is what's responsible for (at least some of) the responses to the issue above.

Thanks! Looks like we need to update Basic Usage with similar note: https://github.com/Moya/Moya/commit/000662a026faf04953163f615663db7b44be6afe

Was this page helpful?
0 / 5 - 0 ratings