简体   繁体   English

在块,核心数据,swift中使用弱,强自我使用

[英]using weak, strong self usage in block, core data, swift

Currently, I am doing a fetch in core data by following 目前,我正在通过以下方式获取核心数据

CoreDataStack.sharedIntance.backgroundContext.performBlock({
    let fetchRequest                =   NSFetchRequest(entityName: "Schedule")
    let sortDescriptor              =   NSSortDescriptor(key: "startTime", ascending: true)

    fetchRequest.sortDescriptors    =   [sortDescriptor]
    var result  =   [Schedule]()

    mainContext.performBlockAndWait { [unowned self] in
        do {
            result = try mainContext.executeFetchRequest(fetchRequest) as! [Schedule]
            success?(result)
        } catch {
            print("error is \(error)")
        }
    }
})

And I am getting an error 我收到了一个错误

Reference to property mainContext in closure requires explicit self to make capture semantics explicit 在闭包中引用属性mainContext需要显式self来使捕获语义显式化

I notice that some of the solutions and they add self for the property in block. 我注意到一些解决方案,他们为块中的属性添加了self

Is it good to do that or should we create a weak or unowned to avoid retain cycle and what is the best way to handle this situation. 这样做是好还是我们应该创建一个weak or unowned以避免保留周期,以及处理这种情况的最佳方法是什么。

Every time you use self in a block, you must consider the future of that block or you can create a reference cycle and leak memory (which is why Swift requires you to be explicit). 每次在块中使用self时,必须考虑该块的未来,或者可以创建引用循环和泄漏内存(这就是Swift要求您明确的原因)。 A reference cycle most often occurs when a block captures (holds a strong reference to) self , some other object holds onto that block, and self holds onto that other object. 当一个块捕获(保持一个强引用) self ,一些其他对象保持在该块上,并且self保持在另一个对象上时,最常发生引用循环。 In that configuration, there is a cycle that contains both self and the other object, so neither can ever deallocate. 在该配置中,有一个包含self和另一个对象的循环,因此都不能解除分配。

This most often happens when the block is a handler for "every time X happens, please do this." 当块是“每次X发生时,请执行此操作”的处理程序时,通常会发生这种情况。 The object that holds that block and does the notification is very often owned by the thing that wants to be notified. 持有该块并执行通知的对象通常由想要通知的事物所拥有。 This is probably the most common kind of reference loop. 这可能是最常见的参考循环。 It is generally resolved by making self weak. 它通常通过self弱化来解决。

performBlock , however, is not this kind of function. 然而, performBlock不是这种功能。 It executes the block and then releases it. 它执行块然后释放它。 In Swift terms it is @noescape (and in the future it may be marked that way and you won't need to use self. in noescape closures). 在Swift术语中它是@noescape (并且在未来它可能被标记为这样,你不需要在noescape闭包中使用self. )。 Until the block executes, self cannot be deallocated, but after the block executes, the cycle is immediately broken. 在块执行之前,不能释放self ,但是在块执行之后,循环立即被中断。 That probably is exactly what you wanted. 这可能正是你想要的。 So using self. 所以使用self. here is fine, and there's no reason to add the complexity of a weak reference. 这里很好,没有理由添加弱引用的复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM