If I use these two classes:
class MotionManager {
class func doTheThing(completionHandler: @escaping (CMDeviceMotion?)->()
) {
print("in")
let manager = CMMotionManager()
manager.deviceMotionUpdateInterval = 0.01
manager.startDeviceMotionUpdates(to:
Hi,
I am having trouble with the below mentioned code.
It crashes in Xcode 9.0 beta 3 (9M174d) but doesn’t crash in Xcode 8.3.3
(8E3004b)
Observations in Xcode 9:
- When Car is changed into a struct and test is made to be mutating, it doesn’t
crash
- When Helper is changed to class, it
In the class method case, the scope of your `manager` object is local the the
class method and it is deallocated immediately as function returns (before the
closure gets a chance to be called).
> On Jul 24, 2017, at 12:22 PM, Nate Birkholz via swift-users
> wrote:
>
>
On 24 Jul 2017, at 07:04, somu subscribe via swift-users
wrote:
> - Is there a bug in my code which is being detected in Xcode 9 ?
Yes. The problem here is that `doSomething(f1:)` is a mutating function, so it
acts like it takes an `inout` reference to `self.helper`.
You can use serial queue.
class Car {
var helper = Helper()
lazy private var queue = DispatchQueue(label: "my queue")
func test() {
helper.doSomething(f1: f1)
}
func f1() {
queue.async {
_ = self.helper.v1 //Crash - Simultaneous accesses
Thank a lot Quinn, your solution to use inout works well without crashing.
Question 1:
- Also changing Helper to a class doesn’t seem to crash. Is that a solution
that wouldn’t cause a crash or just works by chance ?
Background:
Just a little background into what I was trying to achieve (I
> Background:
> Just a little background into what I was trying to achieve (I could be wrong):
>
> - I have a set of classes C1, C2, C3 which has a lot of common code
>
> - I would like to build something that can be reused without exposing the
> implementation details. (I can subclass but
Thanks Joanna Carter, sorry forgot to mention it uses generics, so would
contain associatedtype if protocol is used.
Is the below mentioned approach the usual way to hide implementation details or
is there a better approach ?
With this approach, I had to pass functions of C1 as closures into