2016-03-18 26 views
0

我想找嵌套的塊/閉合,而另一個過程完成關閉主線程的,像這樣夫特:弱引用的存儲&嵌套塊/閉合

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

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

    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) 
        } 
    } 
    
  • SomeNetworkManager

    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() 
    
    } 
    
  • 的HTTPRequest

    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 沒有喜悅..我認爲Defining a Capture List是正確的領域,但至於存儲塊和如何定義它,我不知道我做錯了什麼。

回答

1

在所有的可能性,你要保留週期,因爲完成塊引用的HttpRequest(可能是通過調用對象),引用完成塊的,是這樣的:

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

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

class Owner { 
    var httpReference : HttpReference? 

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

有兩種方法,打破循環,或者通過使用通過使用weak參考一個unowned引用或,兩者都是相當類似,在這種情況下,規範是通過改變使用無主參考自:

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

現在,不保留self,從而打破保留週期。

+0

好的,有意義,但我認爲這意味着我將不得不從頭開始重新思考我的結構。謝謝 – Magoo

+1

也許,但我不這麼認爲。通常所需要的就是思考誰參考了什麼,並通過審慎應用弱點來打破循環。通常的嫌疑人是代表,父母/所有者鏈接以及隱藏鏈接。 –

+0

是的,在向他們添加[無主自我]之後真的解決了這個問題。我有很多事情要做,直到我解決系統性問題爲止,很難看到進展。乾杯:) – Magoo