Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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

deleted 92 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

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

deleted 92 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

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)
 }
 }
}
deleted 239 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96
Loading
added 395 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96
Loading
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96
Loading
default

AltStyle によって変換されたページ (->オリジナル) /