You cannot assume that it will work correctly when used from multiple threads simultaneously. Cocoa mutable collection types are not thread-safe (see Thread Safety Summary), and – as far as I know – the same applies to Swift collections (references: Swift: process Array in parallel using GCD Swift: process Array in parallel using GCD, Swift mutable array thread safety Swift mutable array thread safety, Swift Arrays are not Threadsafe).
You cannot assume that it will work correctly when used from multiple threads simultaneously. Cocoa mutable collection types are not thread-safe (see Thread Safety Summary), and – as far as I know – the same applies to Swift collections (references: Swift: process Array in parallel using GCD, Swift mutable array thread safety, Swift Arrays are not Threadsafe).
You cannot assume that it will work correctly when used from multiple threads simultaneously. Cocoa mutable collection types are not thread-safe (see Thread Safety Summary), and – as far as I know – the same applies to Swift collections (references: Swift: process Array in parallel using GCD, Swift mutable array thread safety, Swift Arrays are not Threadsafe).
Generally, your code looks correct to me, I could not see obvious errors or problems – for the single-threaded usage.
You cannot assume that it will work correctly when used from multiple threads simultaneously. Cocoa mutable collection types are not thread-safe (see Thread Safety Summary ), and – as far as I know – the same applies to Swift collections (references: Swift: process Array in parallel using GCD , Swift mutable array thread safety , Swift Arrays are not Threadsafe ).
In
In
Generally, your code looks correct to me, I could not see obvious errors or problems – for the single-threaded usage.
You cannot assume that it will work correctly when used from multiple threads simultaneously. Cocoa mutable collection types are not thread-safe (see Thread Safety Summary ), and – as far as I know – the same applies to Swift collections (references: Swift: process Array in parallel using GCD , Swift mutable array thread safety , Swift Arrays are not Threadsafe ).
In
I am not sure whatIn
let watcher = DeallocWatcher { [weak self] in
if let v = self?.dict[hashableBox] { self?.block(v) }
in the DeallocWatcher
callback is meant to do, since
public var block: (V)->() = { _ in }
does nothing. However, if this line is omitted, the next line causes a syntax error:
self?.dict[hashableBox] = nil // error: cannot capture 'self' before it is declared
}
This looks like a Swift bug to me, but as a workaround you canself
is (conditionally) unwrapped three times. I would use
optional binding instead so that it is unwrapped only once.
You could also take advantage of optional chainingthe fact that removeValueForKey()
returns the old value (or nil
):
let watcher = DeallocWatcher { [weak self] in
if let s = self {
if let v = s.dict[hashableBox]dict.removeValueForKey(hashableBox) ={
nil s.block(v)
}
}
}
so that the block
property is not needed anymore.
I am not sure what
if let v = self?.dict[hashableBox] { self?.block(v) }
in the DeallocWatcher
callback is meant to do, since
public var block: (V)->() = { _ in }
does nothing. However, if this line is omitted, the next line causes a syntax error:
self?.dict[hashableBox] = nil // error: cannot capture 'self' before it is declared
This looks like a Swift bug to me, but as a workaround you can use optional binding instead of optional chaining:
let watcher = DeallocWatcher { [weak self] in
if let s = self {
s.dict[hashableBox] = nil
}
}
so that the block
property is not needed anymore.
In
let watcher = DeallocWatcher { [weak self] in
if let v = self?.dict[hashableBox] { self?.block(v) }
self?.dict[hashableBox] = nil
}
self
is (conditionally) unwrapped three times. I would use
optional binding instead so that it is unwrapped only once.
You could also take advantage of the fact that removeValueForKey()
returns the old value (or nil
):
let watcher = DeallocWatcher { [weak self] in
if let s = self {
if let v = s.dict.removeValueForKey(hashableBox) {
s.block(v)
}
}
}