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.