Skip to content

Commit cac38e4

Browse files
committed
Added method -allocOwner to SSYManagedObject.
1 parent a8f11e6 commit cac38e4

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

SSYManagedObject.h

+22-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,28 @@ extern NSString* const constKeyNewValue ;
123123
method in subclasses. The implementation simply returns super's owner.
124124
@result The owner of the receiver
125125
*/
126-
- (id)owner ;
126+
- (id)owner;
127+
128+
/*!
129+
@brief Same as -owner, except returns the owner with a retain count of 1
130+
that is not in the autorelease pool – the caller "owns" it
131+
132+
@details This method is recommended instead of -owner whenever the owner is
133+
a transient object which may not be deallocced due to retain cycles with the
134+
receiver. Keep in mind that Core Data can be quite anal-retentive:
135+
136+
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/MO_Lifecycle.html
137+
138+
The reason for the prefix 'alloc' is that, of the four prefixes which
139+
tell the clang objective-c static analyzer that this method returns an object
140+
which the caller "owns", as explained here
141+
142+
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html
143+
144+
'alloc' is the least nonsensical for this case.
145+
*/
146+
147+
- (id)allocOwner;
127148

128149
/*!
129150
@brief Returns whether or not a retained managed object is

SSYManagedObject.m

+10-2
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@ - (id)owner {
222222
return [SSYMOCManager ownerOfManagedObjectContext:[self managedObjectContext]] ;
223223
}
224224

225+
- (id)allocOwner {
226+
id owner;
227+
@autoreleasepool {
228+
owner = [SSYMOCManager ownerOfManagedObjectContext:[self managedObjectContext]];
229+
[owner retain];
230+
}
231+
232+
return owner;
233+
}
234+
225235
- (BOOL)isAvailable {
226236
if ([self isDeleted]) {
227237
return NO ;
@@ -498,10 +508,8 @@ - (NSUInteger)countOfNonNilValues {
498508
#warning Testing snapshot events
499509

500510
+ (NSCountedSet*)awakenedObjects {
501-
NSCountedSet* awakenedObjects = ssyDebugGlobalObject ;
502511
if (!awakenedObjects) {
503512
awakenedObjects = [[NSCountedSet alloc] init] ;
504-
ssyDebugGlobalObject = awakenedObjects ;
505513
NSLog(@"awakenedObjects = %p", awakenedObjects) ;
506514
}
507515

0 commit comments

Comments
 (0)