多个请求时AFNetworking错误

时间:2014-12-27 04:16:10

标签: objective-c swift afnetworking-2

似乎有时当我使用AFHTTPRequestOperation Manager调用包含GET请求的某些方法时,它有时会使用操作管理器不正确地从另一个GET请求中回调到另一个成功的块/闭包。

根据错误日志,它似乎与闭包中的解包选项有关,这似乎是一个关于Swift和Objective C兼容性的讨论问题,但我不确定如何必须解决这个问题我特定的代码集。

我也不想使用Alamo Fire Swift库,我宁愿坚持使用AFNetworking Cocoapod,因为该库的作者说两者都应该可以在Swift项目中使用。

这是我得到的错误日志。

Crashed: com.apple.main-thread
EXC_BREAKPOINT UNKNOWN at 0x0000000100147e48

Thread : Crashed: com.apple.main-thread
0  Kickit                         0x0000000100147e48 Kickit.GroupsDataSource.(logoutForce (Kickit.GroupsDataSource) -> () -> ()).(closure #1) (GroupsDataSource.swift:809)
1  Kickit                         0x00000001001474dc Kickit.GroupsDataSource.(logoutForce (Kickit.GroupsDataSource) -> () -> ()).(closure #1) (GroupsDataSource.swift:823)
2  Kickit                         0x0000000100143074 reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<ObjectiveC.AFHTTPRequestOperation>, @owned Swift.ImplicitlyUnwrappedOptional<Swift.AnyObject>) -> (@unowned ()) to @callee_owned (@in (Swift.ImplicitlyUnwrappedOptional<ObjectiveC.AFHTTPRequestOperation>, Swift.ImplicitlyUnwrappedOptional<Swift.AnyObject>)) -> (@out ()) with unmangled suffix "_constprop0" (GroupsDataSource.swift:365)
3  Kickit                         0x0000000100193934 partial apply forwarder for reabstraction thunk helper from @callee_owned (@in (Swift.ImplicitlyUnwrappedOptional<ObjectiveC.AFHTTPRequestOperation>, Swift.ImplicitlyUnwrappedOptional<Swift.AnyObject>)) -> (@out ()) to @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<ObjectiveC.AFHTTPRequestOperation>, @owned Swift.ImplicitlyUnwrappedOptional<Swift.AnyObject>) -> (@unowned ()) (AppDelegate.swift:615)
4  libdispatch.dylib              0x00000001970253ac _dispatch_call_block_and_release + 24
5  libdispatch.dylib              0x000000019702536c _dispatch_client_callout + 16
6  libdispatch.dylib              0x0000000197029980 _dispatch_main_queue_callback_4CF + 932
7  CoreFoundation                 0x0000000186241fa4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
8  CoreFoundation                 0x000000018624004c __CFRunLoopRun + 1492
9  CoreFoundation                 0x000000018616d0a4 CFRunLoopRunSpecific + 396
10 GraphicsServices               0x000000018f30f5a4 GSEventRunModal + 168
11 UIKit                          0x000000018aaa23c0 UIApplicationMain + 1488
12 Kickit                         0x0000000100193ca4 main (AppDelegate.swift:15)
13 libdyld.dylib                  0x000000019704ea08 start + 4

此处的成功模块是AppDelegate:615

let manager = AFHTTPRequestOperationManager()

                var user_id : Int = NSUserDefaults.standardUserDefaults().objectForKey("user_id") as Int

                if (justSeenEventIDs.count > 0){
                    manager.GET(serverURL+"users/seen_events.json",
                        parameters: ["user_id" : userID()!, "event_ids" : justSeenEventIDs],
                        success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) -> Void in

                            println("JSON: " + responseObject.description)
                            if (responseObject.isKindOfClass(NSDictionary)){


                            }


                        },
                        failure: { (operation: AFHTTPRequestOperation!,error: NSError!) -> Void in
                            println("Error: " + error.localizedDescription + " code: " + "\(error.code)")
                    })
                }

此函数中成功块的开头是GroupsDataSource:365

func loadGroups(completion:(dictionary : NSDictionary?) -> Void){

        let manager = AFHTTPRequestOperationManager()

        manager.requestSerializer = AFJSONRequestSerializer() as AFJSONRequestSerializer

        manager.GET(serverURL+"users/get_state.json",
            parameters: ["user_id" : userID()!],
            success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) -> Void in

                println("JSON: " + responseObject.description)
                if (responseObject.isKindOfClass(NSDictionary)){

                    var dict : NSDictionary = responseObject as NSDictionary

                    if (dict.objectForKey("groups") != nil){
                        self.parseDataFromStateDictionary(responseObject as NSDictionary)

//                        self.parseDataFromGroupsDictionary(responseObject as NSDictionary)
                    }
                    else{
                        completion(dictionary: responseObject as? NSDictionary)
                    }




                }else if(responseObject.isKindOfClass(NSArray)){


                }

                completion(dictionary: nil)
            },
            failure: { (operation: AFHTTPRequestOperation!,error: NSError!) -> Void in


                println("Error: " + error.localizedDescription + " code: " + "\(error.code)")
                completion(dictionary: nil)
        })
    }

最后一个闭包:GroupsDataSource:809。你看到很多CLSNSLogv的原因是因为这个错误似乎只发生在内部分发(生产),而不是通过闪电电缆直接建立设备。如果这有帮助,则仅调用CLSNSLogv 4,5和6,而不是3。

func logoutForce(){

        let manager = AFHTTPRequestOperationManager()

        if (NSUserDefaults.standardUserDefaults().objectForKey("user_id") != nil){

            var user_id : Int = NSUserDefaults.standardUserDefaults().objectForKey("user_id") as Int

            CLSNSLogv("logoutForce 3", getVaList([]))
            manager.GET(serverURL+"users/logout.json",
                parameters: ["user_id" : user_id],
                success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) -> Void in

                    CLSNSLogv("logoutForce 4", getVaList([]))
                    println("JSON: " + responseObject.description)
                    if (responseObject.isKindOfClass(NSDictionary)){

                        CLSNSLogv("logoutForce 5", getVaList([]))
                        var obj : NSDictionary = responseObject as NSDictionary
                        if (obj.objectForKey("no_user_with_auth") != nil){
                            return
                        }

                        CLSNSLogv("logoutForce 6", getVaList([]))
                        NSLog("Here is the dictionary from logging out \(obj.description)")
                        var created : Bool = obj.objectForKey("isLoggedOut") as Bool
                        if (created){
                            CLSNSLogv("logoutForce 7", getVaList([]))
                            NSUserDefaults.standardUserDefaults().removeObjectForKey("phone_number")
                            NSUserDefaults.standardUserDefaults().removeObjectForKey("name")
                            NSUserDefaults.standardUserDefaults().removeObjectForKey("user_id")
                            NSUserDefaults.standardUserDefaults().removeObjectForKey("auth_token")
                            NSUserDefaults.standardUserDefaults().removeObjectForKey("photo_url")
                            NSUserDefaults.standardUserDefaults().removeObjectForKey("photo_updated")
                            GroupsDataSource.sharedInstance.selectedGroup = nil
                            GroupsDataSource.sharedInstance.groupsArray = Array()
                            GroupsDataSource.sharedInstance.groupDictionary = Dictionary()
                            GroupsDataSource.sharedInstance.eventDictionary = Dictionary()
                            GroupsDataSource.sharedInstance.starredEventsArray = Array()
                            logout = false
                            CLSNSLogv("logoutForce 8", getVaList([]))
                        }


                    }else if(responseObject.isKindOfClass(NSArray)){

                    }
                },
                failure: { (operation: AFHTTPRequestOperation!,error: NSError!) -> Void in
                    CLSNSLogv("logoutForce 9", getVaList([]))
                    println("Error: " + error.localizedDescription)
            })
        }
        CLSNSLogv("logoutForce 10", getVaList([]))
        NSUserDefaults.standardUserDefaults().removeObjectForKey("phone_number")
        NSUserDefaults.standardUserDefaults().removeObjectForKey("name")
        NSUserDefaults.standardUserDefaults().removeObjectForKey("user_id")
        NSUserDefaults.standardUserDefaults().removeObjectForKey("auth_token")
        NSUserDefaults.standardUserDefaults().removeObjectForKey("photo_url")
        NSUserDefaults.standardUserDefaults().removeObjectForKey("photo_updated")
        GroupsDataSource.sharedInstance.selectedGroup = nil
        GroupsDataSource.sharedInstance.groupsArray = Array()
        GroupsDataSource.sharedInstance.starredEventsArray = Array()
        logout = false
        CLSNSLogv("logoutForce 11", getVaList([]))                        

    }

0 个答案:

没有答案