FrankZp
FrankZp

Reputation: 2042

iOS - can the number of delegates become a memory issue

I have a very custom project setting which uses one class with a delegate protocol. The main view creates dynamically about 100 objects of this class. For each object the main view controller is set as delegate of the object as the objects need to communicate with the main view controller.

I'm wondering if this can lead to any performance or memory issues caused by the use of the delegation pattern?

Upvotes: 3

Views: 79

Answers (2)

drekka
drekka

Reputation: 21883

Using the delegate or indeed any other pattern does not lead to performance issues or memory leaks. The intent of a pattern is to provide a commonly applied and tested solution to a specific problem, regardless of language, system, memory, etc.

Memory leaks and performance issues can occur in a system in which patterns have been used as easily as any other. But they are not the results of the patterns. They are a result of poor design or memory management.

Fro the case you describe I would not expect any performance issues to arise. Simply because the numbers of objects involved is quite small. However in-proper memory management will lead to memory leaks so you need to be aware of those.

My advice would be to re-read the memory management rules and run your code through the leaks tool in instruments.

Upvotes: 0

gaige
gaige

Reputation: 17481

As long as the same main view is the delegate for all of your objects, the only memory consumption in this pattern are your newly created objects. The delegate references cost nothing in terms of reference, as they are just pointers to your existing main view controller.

With that said, in some circumstances, people do create specialized objects to act as delegates and if you follow that pattern (where you would allocate and maintain a separate delegate object for each "other" object), then you would see consumption and this pattern sometimes leads to leaks depending on how ownership is managed/mismanaged.

But, for the case described above, it should not be a problem.

Upvotes: 4

Related Questions