Swift:在 AFNetworking 调用中关闭的未包装选项

Posted

技术标签:

【中文标题】Swift:在 AFNetworking 调用中关闭的未包装选项【英文标题】:Swift: unwrapped optionals in closure in AFNetworking call 【发布时间】:2014-12-27 16:37:25 【问题描述】:

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

这是下面错误日志中的第 3 行:

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)

第二行:

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)

这是我得到的错误日志。

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。GroupsDataSource: 365 和 GroupsDataSource: 809 的结构类似。

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)")
        )
    

【问题讨论】:

【参考方案1】:

我最终使用 Alamofire 而不是 AFNetworking,这最终解决了我的问题。由 AFNetworking 的创建者 github.com/Alamofire/Alamofire 撰写

【讨论】:

以上是关于Swift:在 AFNetworking 调用中关闭的未包装选项的主要内容,如果未能解决你的问题,请参考以下文章

如何在 swift 中避免由于 AFNetworking 导致的内存泄漏

如何在 Swift 中关闭 ViewController?

firebase 在 Swift 中关闭重载表?

Swift 上的 AFNetworking

如何在 iOS + Swift 中关闭我的应用程序的 Internet 访问

在 Swift 中使用 AFNetworking 接收响应