RestKit / RestKit

RestKit is a framework for consuming and modeling RESTful web resources on iOS and OS X
Apache License 2.0
10.18k stars 2.1k forks source link

Method managedObjectRequestOperationWithRequest is not called #2385

Open inksword opened 8 years ago

inksword commented 8 years ago

When I am trying to subclass RKObjectManager, I found the following comments:

  • managedObjectRequestOperationWithRequest:managedObjectContext:success:failure: - Used to construct all managed object request operations for the manager. Provide a subclass implementation if you wish to alter the behavior of all managed object request operations.
  • appropriateObjectRequestOperationWithObject:method:path:parameters: - Used to construct all object request operations for the manager, both managed and unmanaged. Invokes either objectRequestOperationWithRequest:success:failure: or managedObjectRequestOperationWithRequest:managedObjectContext:success:failure: to construct the actual request. Provide a subclass implementation to alter behaviors for all object request operations constructed by the manager.

However, when I overwrite managedObjectRequestOperationWithRequest:managedObjectContext:success:failure, the method is never called, because its never called by appropriateObjectRequestOperationWithObject:method:path:parameters: in RKObjectManager.

sfoulston commented 8 years ago

+1 Just encountered this myself also. There is no line of code in RKObjectManager that calls managedObjectRequestOperationWithRequest(_:managedObjectContext:success:failure:), so subclassing to change the behaviour of managed object request operations doesn’t work as described in the docs.

The workaround for now is to instead override appropriateObjectRequestOperationWithObject(_:method:path:parameters:) and perform the customisation if the request op returned by super is of type RKManagedObjectRequestOperation:

override func appropriateObjectRequestOperationWithObject(object: AnyObject!, method: RKRequestMethod, path: String!, parameters: [NSObject : AnyObject]!) -> AnyObject! {

    let requestOp = super.appropriateObjectRequestOperationWithObject(object, method: method, path: path, parameters: parameters)

    if let managedObjectRequestOp = requestOp as? RKManagedObjectRequestOperation {
        // Customise the managed object request op...
    }

    return requestOp
}