-1

Did some research on the topic but still don't get it.

NSTimeInterval is always 0 when using [NSDate timeIntervalSinceDate:NSDate].

Example.

- (NSNumber*) elapsed
    {
    __block NSDate* currentDate;
    dispatch_async(dispatch_get_main_queue(),
        ^{
        currentDate = [NSDate date];
        });
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.transactionStartedAt];
    _elapsed = @(timeInterval*1000);
    return _elapsed;
    }

You can see I use dispatch_async in the main queue because this getter property is accessed from the background queue and the [NSDate date] returns nil, this is how I figured to resolve this.

Then you can see that I'm looking for the timeinterval and convert to milliseconds between the two dates.

EDIT: The time interval problem solved with this code.

- (NSNumber*) elapsed
    {
    NSDate* currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.transactionStartedAt];
    NSLog(@"TimeInterval: %f", timeInterval);
    return @(timeInterval*1000);
    }

This is actually a getter in the object so if I change the code to

- (NSNumber*) elapsed
    {
    NSDate* currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.transactionStartedAt];
    NSLog(@"TimeInterval: %f", timeInterval);
    return _elapsed = @(timeInterval*1000);
    }

I get EXC_BAD_ACCESS fatal exception.

I guess I have problem changing the value from another background queue that the one that the object is created!

Regards.

George Taskos
  • 8,324
  • 18
  • 82
  • 147

2 Answers2

3

At the time you call timeIntervalSinceDate, currentDate is UNDEFINED (it hasn't been assigned yet). I guess in this case the compiler decided to make it nil. And calling a method (sending a message) on a nil object always returns 0.

Dispatching a block asynchronously makes the block to be executed sometimes later, in this case probably after the method has returned. In this case it will probably do some very bad things to the memory because it will assign to a variable on stack which will be already invalid.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • I get what you say. Now I removed the GCD which I thought anyway it was bad for this reason and I get timeinterval correctly. Now I implemented the getter of the property and set as you see the generated _elapsed field and when return the value I get BAD_EXC. If I just return the timeinterval value it's working fine. I just want to clear why I get BAD_EXC. Any thoughts? – George Taskos Dec 20 '13 at 14:58
  • You would have to share your code again. However, your code is a bit strange. You have a getter which actually changes your object (assigning into `_elapsed`). So, who knows what else is happening. – Sulthan Dec 20 '13 at 15:03
  • I actually change the _elapsed value. Edited the question. – George Taskos Dec 20 '13 at 15:10
2

so the problem is your block gets called after the

    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.transactionStartedAt];
_elapsed = @(timeInterval*1000);
return _elapsed;
}

when you use dispatch_async the code gets called on the next run loop loop. try to debug this method to see what i mean

kap
  • 842
  • 4
  • 16
  • Yes, I get that. Hmmm then my problem is that I get a nil when trying to get the current date with [NSDate date] now. – George Taskos Dec 20 '13 at 14:52
  • @TaskosGeorge I guess your problem is in debugging. Getting `nil` from `[NSDate date]` is improbable. – Sulthan Dec 20 '13 at 14:56