Simultaneous accesses to 0x10f10df48, but modification requires exclusive access

Viewed 4102

I'm working on swift4 migration and this warning (that make my app very slow) appears.

Simultaneous accesses to 0x10f10df48, but modification requires exclusive access.

on line

else if (context == &KVOContext && keyPath == contentSizeKeyPath && object as? UIScrollView == scrollView) {

Can't found how to solve it.

1 Answers

https://github.com/apple/swift-evolution/blob/master/proposals/0176-enforce-exclusive-access-to-memory.md In this link you can see the conflict of &KVOContext

like this:

// CONFLICT.  Passing 'x' as an inout argument is a write access for the
// duration of the call.  Passing the same variable twice means performing
// two overlapping write accesses to that variable, which therefore conflict.
swap(&x, &x)

extension Int {
  mutating func assignResultOf(_ function: () -> Int) {
    self = function()  
  }
} 

I solved it by

struct Pair {
  var x: Int
  var y: Int
}

class Paired {
  var pair = Pair(x: 0, y: 0)
}

let object = Paired()
swap(&object.pair.x, &object.pair.y)

My code is

    class Paired {
        var pair = Pair(x : "PullToRefreshKVOContext")
    }

    let objects = Paired()
    override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if (context == &objects.pair.x && keyPath == contentOffsetKeyPath && object as? UIScrollView == scrollView) {
...
}
Related