Comments (11)
Can you explain what it's supposed to do? In particular, I don't understand the point of using dispatch_once
to set an associated object like that.
from maobjcruntime.
Yeah that pattern was popularized for instantiating singletons instead of checking nil
. My FIIcon
category snippet uses it for just that. It shouldn't cost any memory or computation until first access.
My UIView
category snippet is part of an API that proxies UIAppearance
selectors directly to arbitrary [sub]layers for better styling since CALayer
supports CSS-like method selectors. In this case it facilitates getting arbitrary additional sublayers for fancy filters or whatever. GCD ensures they're only added as desired even if by an UIAppearance
proxy.
In practice I typically only instantiate ivars once and modify their state thereafter. But I instantiate them in lifecycle methods that categories shouldn't replace. My Associated Object API provides both storage and lazy instantiation.
from maobjcruntime.
I'm familiar with the pattern, but this isn't a singleton. You're using a global dispatch_once
to control the adding of an associated object to a single arbitrary instance in your -[UIView sublayer]
. This doesn't make any sense to me, and providing an API that takes a dispatch_once_t *
in general does not seem like the right approach.
from maobjcruntime.
Ah quite right my sublayer one is messed up. I'll fix it. In essence it needs a unique key per instance.
from maobjcruntime.
That doesn't really address the overall concern, though. What is the point of using dispatch_once
to gate access to an associated object like this? dispatch_once
is generally used for globals, but associated objects are inherently per-instance, so it seems like a complete mismatch.
from maobjcruntime.
Although it's not a singleton it is truly a multiton. Because categories can't have ivars the only alternative is still a nil
guard or + load()
without lazy instantiation. GCD just "feels" better to me. It's funny I ran across your blog googling about it just now. What would you do besides a nil
guard? PS thx :)
from maobjcruntime.
The thing is that I don't see how the GCD solution works. There appear to only be two cases:
- There's a single object, ever.
- There are multiple objects.
Case 1 is basically the standard singleton pattern and can be solved without associated objects, by just using a global variable. In case 2, dispatch_once
doesn't work, because you need to run the code more than once (i.e. once for each object). Case 1 can use dispatch_once
but doesn't need associated objects. Case 2 needs associated objects but can't use dispatch_once
. So I don't see where it's ever useful to have an API that combines them like this. Can you give an example of where it would be useful?
from maobjcruntime.
Wow what's ridiculous is I'm using a static variable for a singleton dictionary elsewhere in that same class. Guess I had tunnel vision. If I have a reasonable multiton solution I'll share. My use case is implementing category properties like my sublayers without nil
checks. Closing ticket; great feedback.
from maobjcruntime.
OK, cool, glad we figured out what the disconnect was. I think you'd want to use associated objects still, but I'm not sure if there's a good way to lazily initialize them. You can do a check-and-set, but it's not thread safe without external synchronization....
from maobjcruntime.
I tried concocting a strategy to use the pointer storage returned by objc_getassociatedobject(self, key)
to store a dispatch_once_t
and failed. I just wrapped it in @synchronized
for now.
from maobjcruntime.
Just wanted to note that anytime I've googled about Obj-C sticking points your blog has unanimously been the authoritative source of documentation. If you had a book I'd read it
from maobjcruntime.
Related Issues (9)
- Attribution for spawned project HOT 6
- How to get the use of a app traffic?
- Definition of 'Protocol' must be imported from module 'ObjectiveC.Protocol' before it is required HOT 2
- Does it support arc? HOT 1
- How to better use of PLCrashReporter? HOT 1
- TestProtocolQuery fails for i386 HOT 1
- Deployable to App Store? HOT 2
- Methods with primitive arguments HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from maobjcruntime.