GCDTimer | Well-tested GCD Timer in Swift | iOS library
kandi X-RAY | GCDTimer Summary
kandi X-RAY | GCDTimer Summary
Well tested Grand Central Dispatch (GCD) Timer in Swift. Checkout the test file.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of GCDTimer
GCDTimer Key Features
GCDTimer Examples and Code Snippets
Community Discussions
Trending Discussions on GCDTimer
QUESTION
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 1 * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.timer, ^{
NSLog(@"GCDTimer %@", [NSThread currentThread]);
});
dispatch_resume(self.timer);
...ANSWER
Answered 2020-Aug-04 at 08:43The queue has a couple of threads and will select one of these to use when the timer fires. If the queue is concurrent (as is the global queue) then it will select one even if another one is still running. If the queue is serial then it also will select from multiple threads, but only one will be running at a time.
QUESTION
OK. I have been reading the excellent "Advanced Swift" book by the objc crew. It's pretty much awesome (but a tough read). It has got me reevaluating the way I've done things in Swift for the last 4 years. I won't link it here, because I don't want this question flagged as spam.
One thing that I'm doing is building up a toolbox of good general-purpose utilities (or rewriting the ones I already have).
One of these tools is a basic GCD Timer.
In the playground that I'll attach, is a basic timer class, along with a testing class and a couple of test implementations.
In one of them, I register a delegate, and the other, I do not. This makes a difference in whether or not the deinit is called on the main test instance.
It looks like the GCD timer hangs onto a strong reference to the delegate, even when I explicitly remove that reference.
As you will see, even when the timer is completely deallocated (its deinit is called), it retains a strong reference to its delegate object (so the main deinit is never called).
Line 257 is interesting. If you comment that out, then the timer keeps firing, even though it has been dereferenced. I can understand that, as I assume that the GCD timer retains a strong reference to its eventHandler. I might be able to avoid that by using an inline closure, instead of referencing an instance method. It doesn't really matter that much, as explicitly calling invalidate() is perfectly fine.
However, it does make me wonder what other strong references are being kept. Changing to an inline closure does not address the main issue; namely, that the main context retains a context, even though it seems to be orphaned.
I'm wondering if someone could explain to me how come the main (iAmADelegate
) instance is retained. I spent all day yesterday trying to figure it out.
UPDATE It looks like this does not happen in an actual app context. Here is a very basic applet that demonstrates the same tests in the context of an iOS app.
For the record, here is the console for what I get. If you run the playground, you should get the same:
...ANSWER
Answered 2019-Jan-11 at 14:18The issue is one that could not have been predicted without actually creating a project.
It seems that the Playground Swift engine deals with reference-counting and scope differently from the app engine. It hangs onto things longer. I could probably get it to behave correctly by wrapping the whole thing in another scope that gets dereferenced.
Unfortunately, the "Mystery Spot" answer doesn't apply here, as that's for the higher-level Timer method. This is a lower-level GCD timed task.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install GCDTimer
Execute carthage update
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page