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

result.operation is nil when calling AFMotion::JSON.get #24

Closed
larsudengaard opened this issue Sep 24, 2013 · 6 comments
Closed

result.operation is nil when calling AFMotion::JSON.get #24

larsudengaard opened this issue Sep 24, 2013 · 6 comments

Comments

@larsudengaard
Copy link

After upgrading to the latest version of RubyMotion, the following code does not work anymore, because result.operation is 'nil'.

We are using RubyMotion v2.9 and AFMotion 0.9.0. The problem started occurring when we upgrade RubyMotion from v2.3 to v2.9.

  def initAndGet(url)
    @isFailed = false
    request = self.createRequest url
    AFMotion::JSON.get(request) do |result|
      handleResponse(request, result)
    end
  end

  def handle_response(request, result)
    response = result.operation.response
    if response
      if (response.statusCode() >= 200 and response.statusCode() <= 299)
        onSuccess(result, response)
      else
        onError(result, response.statusCode())
        @isFailed = true
      end
    else
      onConnectionError(request)
    end

    @delegate.request_done(self) if @delegate and @delegate.respond_to? :request_done
    onDone
  end
@clayallsopp
Copy link
Collaborator

  • request (returned from createRequest) is a String right?
  • So if you downgrade to v2.3, run this code with no changes, it works?
  • Is it possible to upload a reproducible example? Have you tried running AFMotion's requests against other endpoints than whatever request is?

@larsudengaard
Copy link
Author

No, actually it create and NSMutableURLRequest

  def createRequest(url)
    request = NSMutableURLRequest.requestWithURL(NSURL.URLWithString(url),
                                                 cachePolicy: NSURLRequestReloadIgnoringLocalCacheData,
                                                 timeoutInterval: 300)

    apiVersion = NSBundle.mainBundle.objectForInfoDictionaryKey('ApiVersion')
    request.setValue(apiVersion, forHTTPHeaderField: 'ApiVersion')

    request
  end

I think the problem is how the AFMotion::Operation::JSON.for_request closes over the variable operation.
In AFMotion::Operation::HTTP.for_request you initiate the operation variable before closing over it in the succes and failure blocks. Im am no expert on how closures work in ruby though :-)

Reimplementing AFMotion::Operation::JSON.for_request to look more like the HTTP.for_request solves the problem for me:

  def json_get_request(request, &callback)
    operation = AFJSONRequestOperation.alloc.initWithRequest(request)
    operation.setCompletionBlockWithSuccess(
        lambda { |operation, responseObject|
          result = AFMotion::HTTPResult.new(operation, responseObject, nil)
          callback.call(result)
        },
        failure: lambda {|operation, error|
          result = AFMotion::HTTPResult.new(operation, nil, error)
          callback.call(result)
        }
    )
    operation
  end

@larsudengaard
Copy link
Author

And to answer your questions: :-)

request (returned from createRequest) is a String right?
No, NSMutableRequest
So if you downgrade to v2.3, run this code with no changes, it works?
Yes
Is it possible to upload a reproducible example? Have you tried running AFMotion's requests against other endpoints than whatever request is?
I think i might how found a fix for the problem, see the above comment. If not, I will try and make a reproducible example tonight (CET).

@clayallsopp
Copy link
Collaborator

Ah I see; yeah the code-paths when passing NSURLRequests aren't very well tested. If your fix works, definitely submit a pull request! (with a test, preferably :) )

@larsudengaard
Copy link
Author

What is the alternative method? :-)
I'll try and get around making a pull-request (and test ;-)) tonight.

@clayallsopp
Copy link
Collaborator

The alternative method is to passing a String, which will get converted to an NSURLRequest: https://github.com/usepropeller/afmotion/blob/master/lib/afmotion/http.rb#L7. Somehow I guess this triggers a different memory behavior than simply passing one =\

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants