retain

Android Fragments Retaining Data

久未见 提交于 2019-11-28 08:07:48
问题 How does one best retain data within a Fragment as its activity goes through an onCreate/Destroy cycle like from Rotation? In our setup we have potentially large lists loaded from our servers into the fragments custom list adapter and we want to smooth out the UX by not making them reload on rotation. The problem we had with setting the fragment retainInstance=true; is that our adapter has a reference to the context of the original activity and would therefore leak memory. Could we just store

property “assign” and “retain” for delegate

一曲冷凌霜 提交于 2019-11-28 07:34:18
For iOS developers, delegates are used almost everywhere. And seems like that we need to use "assign" instead of retain for a delegate like this @property(assign) id delegate; The reason is to avoid the circular loop issue Why are Objective-C delegates usually given the property assign instead of retain? I saw a lot of code and they still used "retain". So the question here is will we still get the circular loop issue if we use retain for a delegate? Thanks vfn The documentation says: Retaining an object creates a strong reference, and an object cannot be deallocated until all of its strong

Why retain a static variable?

ぃ、小莉子 提交于 2019-11-28 06:27:59
Isn't it unnecessary to retain a static variable since it stays around for the duration of the program, no matter if you release it? See this code: https://github.com/magicalpanda/MagicalRecord/blob/master/Source/Categories/NSManagedObjectContext+MagicalRecord.m#L24-29 I'm assuming you mean a static object pointer, such as static NSString *foobar; . Such variables indeed have a lifetime as long as the application, but the variables we're talking about are pointers only . In Objective-C, objects are always dynamically allocated, and so we always address them with a pointer to their type, but

Release or set to nil retained members

帅比萌擦擦* 提交于 2019-11-28 05:44:20
问题 Is it better to set my retained member vars to nil or to release them when I am cleaning up? Setting a retained var to nil seems a safer way to release an object without risking a double release call on it. Update: Let me elaborate that I'm referring to member vars that have been set to have the retain property i.e.: @property (nonatomic, retain) SomeClass* mInstanceVar; 回答1: It's best practice to release your instance variables first, and then set them to nil in your -dealloc method. I

Objective C release, autorelease, and data types

时光怂恿深爱的人放手 提交于 2019-11-28 03:50:56
I'm new to memory managed code but I get the idea pretty well. On taking my app through the leaks tool in XCode, I noticed I only had to clean up my custom objects, but not dynamically created arrays for example, so I figured those data types are autoreleased - makes sense since I only had to release the arrays I used as properties that had a (retain) on them. Then I noticed something peculiar : I was getting a leak on a certain array initialized like this : NSMutableArray *removals = [NSMutableArray new]; but not a similar one NSMutableArray *removals = [NSMutableArray arrayWithCapacity:9];

Objective-C retain counts clarification

孤者浪人 提交于 2019-11-27 17:03:04
问题 I kind of understand what's retain counts for. But not totally. I looked on google a lot to try to understand but still I don't. And now I'm in a bit of code (I'm doing iPhone development) that I think I should use them but don't know totally how. Could someone give me a quick and good example of how and why using them? Thanks! 回答1: The best explanation I ever heard was from Aaron Hillegass: Think of the object as a dog. You need a leash for a dog to keep it from running away and disappearing

How is retain setter implemented with @synthesize?

做~自己de王妃 提交于 2019-11-27 06:14:51
问题 I have the following in the header: @property (nonatomic, retain) UIView *overlay; And in the implementation: @synthesize overlay; Then: UIView *tempOverlay = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 70.0f, 150.0f, 310.0f)]; self.overlay = tempOverlay; [tempOverlay release]; Isn't the tempOverlay variable above unnecessary? Can't I just do: self.overlay = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 70.0f, 150.0f, 310.0f)]; 回答1: A synthesized retained setter looks like : - (void

Why retain a static variable?

自作多情 提交于 2019-11-27 05:40:28
问题 Isn't it unnecessary to retain a static variable since it stays around for the duration of the program, no matter if you release it? See this code: https://github.com/magicalpanda/MagicalRecord/blob/master/Source/Categories/NSManagedObjectContext+MagicalRecord.m#L24-29 回答1: I'm assuming you mean a static object pointer, such as static NSString *foobar; . Such variables indeed have a lifetime as long as the application, but the variables we're talking about are pointers only . In Objective-C,

Non-retaining array for delegates

五迷三道 提交于 2019-11-27 02:58:18
In a Cocoa Touch project, I need a specific class to have not only a single delegate object, but many of them. It looks like I should create an NSArray for these delegates; the problem is that NSArray would have all these delegates retained, which it shouldn't (by convention objects should not retain their delegates). Should I write my own array class to prevent retaining or are there simpler methods? Thank you! I found this bit of code awhile ago (can't remember who to attribute it to). It's quite ingenius, using a Category to allow the creation of a mutable array that does no retain/release

Objective-C 101 (retain vs assign) NSString

a 夏天 提交于 2019-11-27 02:51:21
A 101 question Let's say i'm making database of cars and each car object is defined as: #import <UIKit/UIKit.h> @interface Car:NSObject{ NSString *name; } @property(nonatomic, retain) NSString *name; Why is it @property(nonatomic, retain) NSString *name; and not @property(nonatomic, assign) NSString *name; ? I understand that assign will not increment the reference counter as retain will do. But why use retain , since name is a member of the todo object the scope of it is to itself. No other external function will modify it either. Chuck There's no such thing as the "scope of an object" in