Article

iOS Memory Management and Multithreading

Topic: Career DevelopmentPublished November 28, 2017

Legacy signals

Legacy popularity: 657 legacy views

This post contains my perusing notes of the book , from the systems of ARC to the utilization of Blocks and Grand Central Dispatch. Memory Management Actions in Objective-C - Reference Counting Activity for Objective-C Object Objective-C Method Make and have responsibility for/new/duplicate/mutableCopy gathering Take responsibility for Surrender it release Discard it dealloc +(id)alloc implementation.m + (id) alloc { return [self allocWithZone: NSDefaultMallocZone()]; } + (id) allocWithZone: (NSZone*)z { return NSAllocateObject (self, 0, z); } struct obj_layout { NSUInteger held; }; inline id NSAllocateObject (Class aClass, NSUInteger extraBytes, NSZone *zone) { int estimate =/* required size to store the question */ id new = NSZoneMalloc(zone, estimate); memset(new, 0, estimate); new = (id)&((struct obj_layout *)new)[1]; } The NSAllocateObject work calls NSZoneMalloc to apportion a memory territory. From that point forward, the territory is loaded with zero and the range pointer is returned. Objective-C now disregards zones, which was utilized to avoid memory discontinuity. The alloc technique would now be able to be changed as: demo.m struct obj_layout { NSUInteger held; }; + (id) alloc { int estimate = sizeof(struct obj_layout) + size_of_the_object; struct obj_layout *p = (struct obj_layout *)calloc(1, estimate); return (id)(p + 1); } The alloc strategy restores a memory piece loaded with zero containing a struct obj_layout header, which has a variable "held" to store the quantity of references(reference tally). hold get reference tally an incentive by calling retainCount: demo.m id obj = [[NSObject alloc] init]; NSLog(@"retainCount=%d", [obj retainCount]); /* * retainCount=1 is shown. */ Execution of hold: demo.m - (id) retain{ NSIncrementExtraRefCount(self); return self; } inline void NSIncrementExtraRefCount(id anObject) { in the event that (((struct obj_layout *)anObject)[-1].retained == UINT_MAX - 1) [NSException raise: NSInternalInconsistencyException arrange: @"NSIncrementExtraRefCount() asked to increase excessively far"]; ((struct obj_layout *)anObject)[-1].retained++; } at the point when the variable "held" floods, it's increased. discharge demo.m - (void) discharge { on the off chance that (NSDecrementExtraRefCountWasZero(self)) [self dealloc]; } BOOL NSDecrementExtraRefCountWasZero(id anObject) { on the off chance that (((struct obj_layout *)anObject)[-1].retained == 0) { return YES; } else { ((struct obj_layout *)anObject)[-1].retained- - ; restore NO; } } "held" decremented. dealloc demo.m - (void) dealloc { NSDeallocateObject (self); } inline void NSDeallocateObject(id anObject) { struct obj_layout *o = &((struct obj_layout *)anObject)[-1]; free(o); } a chunck of memory desposed. autorelease like "programmed variable" in C, which is discarded consequently when the execution leaves the degree. autorelease implies when execution leaves a code hinder, the discharge strategy is approached the question naturally. demo.m - (id) autorelease { [NSAutoreleasePool addObject:self]; } ARC(Automatic Reference Counting) With ARC, "id" and protest sort factors must have one of the accompanying possession qualifiers: _strong, _weak, __unsafeunretained, _autoreleasing Possession is appropriately overseen by factor scope, as well as by assignments between factors, which are qualified with __strong. Utilize __weak to maintain a strategic distance from roundabout references: A __weak possession qualifier gives a frail reference. A powerless reference does not have responsibility for protest. demo.m id __strong obj0 = [[NSObject alloc] init]; id __weak obj1 = obj0; /* * variable obj1 has a feeble reference of the made protest */ _unsafe_unretained: don't utilize it unless you need to help before iOS5, it may leave a dangling pointer. utilize _weak. Notwithstanding, __weak is only for pointers that ought to be focused, not for primitive sorts. _unsafe_unretained is valuable for circumstances where you have a protest pointer in a circumstance where ARC doesn't have enough control to securely oversee memory, as within a consistent C struct. That is one reason there's the "no question pointers in C struct" confinement, unless you enrich that point with _unsafe_unretained to tell the compiler you what you're doing. It's likewise used to break solid reference cycles (a.k.a. hold cycles) with obstructs under some particular conditions. However, for 99.9% of day by day work, feeble works awesome and you can disregard __unsafe_unretained. Source Pieces A Block is created from the Block strict beginning with "^". Also, the Block is relegated to the variable "blk". Obviously you can relegate the incentive to different factors of the Block sort. Squares are Anonymous capacities together with automatic(local)variables. Presentations (source) As a neighborhood variable: returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...}; As a property: @property (nonatomic, duplicate) returnType (^blockName)(parameterTypes); As a strategy parameter: (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName; As a contention to a strategy call: [someObject someMethodThatTakesABlock: ^returnType (parameters) {...}]; As a typedef: typedef returnType (^TypeName)(parameterTypes); TypeName blockName = ^returnType(parameters) {...}; block.m int (^blk1)(int) = blk; int (^blk2)(int); blk2 = blk1; /*Functions can take contentions of Block type.*/ void func(int (^blk)(int)) { /*Also, capacities can restore a Block.*/ int (^func()(int)) { return ^(int count){return check + 1;}; } Utilizing typedefs: block.m typedef int (^blk_t)(int); Amazing Central Dispatch The most effective method to utilize: characterize assignments you need to execute and add them to a suitable dispatch line. GCD.m dispatch_async(queue_for_background_threads, ^{ /* * Here, handling a tedious errand * * The errand is finished. At that point, utilize the outcome on the primary string as takes after. */ dispatch_async(dispatch_get_main_queue(), ^{ /* * Here, undertakings that work just on the primary, for example, * refreshing client inteface, and so forth. */ }); }); Dispatch Queues A dispatch line is a line to store undertakings to be executed (in the additional, first in first out request). More subtle elements visit http://infocampus.co.in/ios-training-in-bangalore.html

Further reading

Further Reading

4 total

Article

If you've ever wanted to create the ultimate custom draft experience for your Magic: The Gathering playgroup, building a Magic Cube is one of the most rewarding projects you can take on. An MTG Cube is a curated collection of cards designed to be drafted over and over again, offering endless replayability and a deeply personal expression of what you love about the game. Whether you prefer blazing-fast aggro strategies, intricate combo lines, or grindy control mirrors, your cu

February 23, 2026

Article

Key Takeaways: Family Nurse Practitioners (FNPs) provide comprehensive primary care, bridging the gap between traditional nursing and advanced clinical practice. Expanding the scope of practice enables FNPs to enjoy greater autonomy, higher earning potential, and improved access to care. Specialization, leadership, academic, telehealth, and entrepreneurial roles offer diverse career pathways and professional growth. FNPs play a critical role in shaping healthcare delivery, in

February 9, 2026

Article

The online graduate certificate in drug discovery and development provides a strategic educational platform for professionals preparing to lead innovation in pharmaceutical science. With flexible, asynchronous learning and a curriculum grounded in applied research and regulatory principles, this program at The University of North Texas Health Science Center at Fort Worth is well-suited for individuals seeking to expand their competencies while balancing academic, clinical, or

July 18, 2025

Article

Life often feels like a delicate balancing act between work, family, social obligations, and the everyday chores that never seem to end. One of the most time-consuming of these is keeping a home clean and organized. Recent trends and statistics reveal that more people are turning to professional home cleaning services as a practical solution to free up their schedules and reduce stress. My Cleaning Angel stands at the forefront of this movement, offering reliable home cleanin

May 1, 2025