gpt4 book ai didi

Swift:弱引用存储和嵌套 block /闭包

转载 作者:行者123 更新时间:2023-11-30 10:05:46 26 4
gpt4 key购买 nike

我希望嵌套一个 block /闭包,同时另一个进程像这样从主线程完成

typealias FirstBlock = (jsonDictionary:NSDictionary?,errorCode:NSString?) -> Void

typealias SecondBlock = (complete:Bool?,errorCode:NSString?,dictionary:NSDictionary?) -> Void
  • Controller

        func startPoint {

    SomeNetworkManager.sharedInstance.firstProcess(self.someDictionary) { (complete, errorCode, dictionary) -> Void in

    // I want to get here with a strong reference to these objects in this class only
    print(complete,errorCode,dictionary)
    }
    }
  • 一些网络管理器

    func firstProcess(dictionary:NSDictionary?, completion:SecondBlock?) {

    let request = HTTPRequest.init(requestWithPath:"path", httpMethod: .post) { (jsonDictionary, errorCode) -> Void in

    let organisedDictionary:NSMutableDictionary = NSMutableDictionary()
    // Some processing of the json into a new dictionary

    dispatch_async(dispatch_get_main_queue()) {

    if errorCode == nil {

    completion!(complete:true,errorCode:nil,dictionary:organisedDictionary)
    }
    else {

    completion!(complete:false,errorCode:errorCode,dictionary:nil)
    }
    }
    }

    request.postDataDictionary = refinementsDictionary as! NSMutableDictionary
    request.request()

    }
  • HTTP 请求

    var processBlock:FirstBlock?

    init(requestWithPath path:NSString, httpMethod method:HTTPMethod, andProcessBlock block:FirstBlock) {

    super.init()

    self.requestURL = NSURL(string:path as String);
    self.responseData = NSMutableData()
    self.processBlock = block

    switch (method) {
    case .post:
    self.httpMethod = kPost
    break;
    case .put:
    self.httpMethod = kPut
    break;
    default:
    self.httpMethod = kGet
    break;
    }

    }

    // An NSURLConnection goes off, completes, I serialise the json and then...

    func completeWithJSONDictionary(jsonDictionary:NSDictionary) {

    self.processBlock!(jsonDictionary:jsonDictionary,errorCode:nil)
    self.processBlock = nil
    }

我遗漏了一些关于 ARC 保留周期的基本知识,因为每次调用其中一个时,我都会遇到内存泄漏..我已经看过了 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html没有喜悦..我认为定义捕获列表是正确的区域,但至于存储 block 以及如何定义它,我不知道我做错了什么。

最佳答案

很可能,您会遇到保留周期,因为完成 block 引用 HttpRequest(可能通过调用对象),引用完成 block ,例如:

class HttpReference {
let completion : ()->()

init(completion:()->()) {
self.completion = completion
}
}

class Owner {
var httpReference : HttpReference?

func someFunction() {
httpReference = HttpReference() {
print(self.httpReference)
}
}
}

有两种方法可以打破循环,要么使用unowned引用,要么使用weak引用,两者都非常相似,在这种情况下,规范是通过更改来使用对 self 的无主引用:

func someFunction() {
httpReference = HttpReference() { [unowned self] in
print(self.httpReference)
}
}

现在,self 未被保留,从而打破了保留周期。

关于Swift:弱引用存储和嵌套 block /闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36089691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com