I tweaked Tom's approach slightly, to get the following design methodology: Adding IVars to a Class (and it's subclasses). A future post will explain in detail why I needed these extra parameters, but briefly, I want to extend NSManagedObjectContext, a class I do not own, with methods that short cut the work required to build and execute fetching from Core Data. Adding a little bit of state and/or default values to the context (instead of passing that data into/out of the method calls) simplifies matters. Making IVar access transparent eases the burden on the caller. Since I don't own the class definition, I needed another method to associate data and Tom's post pointed the way.
Example Code
CompactFetch.h - Category declaration
@interface NSManagedObjectContext (CompactFetch)
@property (assign,nonatomic) NSUInteger defaultFetchLimit;
@property (retain,nonatomic,readonly) NSError* lastError;
@end
@property (assign,nonatomic) NSUInteger defaultFetchLimit;
@property (retain,nonatomic,readonly) NSError* lastError;
@end
In our header, we create a category, CompactFetch, for NSManagedContext. Notice the two properties created. These property declarations do not use @synthesize for their implementations. The nice thing about making them properties, is that code can use dot-notation to set or get their values. In addition, the code is KVC compliant. Note, you can use objects here, just be careful with assign & retain.
CompactFetch.m - IVar class declaration
@interface CompactFetchIVars : NSObject
@property (assign,nonatomic) NSUInteger defaultFetchLimit;
@property (retain,nonatomic) NSError* lastError;
+ (CompactFetchIVars*)fetch:(NSManagedObjectContext*)moc;
@end
@property (assign,nonatomic) NSUInteger defaultFetchLimit;
@property (retain,nonatomic) NSError* lastError;
+ (CompactFetchIVars*)fetch:(NSManagedObjectContext*)moc;
@end
Next, we create a class, CompactFetchIVars, which mirrors the @property declarations on the Category. Again, be sure to match up the assign & retain annotations. Although the property names need not be the same, keeping them that way reduces confusion.
Look at the class method, +fetch:, the heart of our design. It takes an NSManagedObjectContext and returns an instance of our class. We use this class method to attach new ivars (new data) onto the Core Data class. By the way, this @interface resides in the .m file, there's no reason to expose the caller to this detail.
Now, for CompactFetchIVars's class definition:
CompactFetch.m - IVar class definition
@implementation CompactFetchIVars
@synthesize defaultFetchLimit, lastError;
+ (CompactFetchIVars*)fetch:(NSManagedObjectContext*)moc
{
static void *compactFetchIVarKey = &compactFetchIVarKey;
CompactFetchIVars *ivars = objc_getAssociatedObject(moc, &compactFetchIVarKey);
if (ivars == nil) {
ivars = [[CompactFetchIVars alloc] init];
objc_setAssociatedObject(moc, &compactFetchIVarKey, ivars, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[ivars release];
}
return ivars;
}
- (id)init
{
self = [super init];
return self;
}
- (void)dealloc
{
self.lastError = nil;
[super dealloc];
}
@end
@synthesize defaultFetchLimit, lastError;
+ (CompactFetchIVars*)fetch:(NSManagedObjectContext*)moc
{
static void *compactFetchIVarKey = &compactFetchIVarKey;
CompactFetchIVars *ivars = objc_getAssociatedObject(moc, &compactFetchIVarKey);
if (ivars == nil) {
ivars = [[CompactFetchIVars alloc] init];
objc_setAssociatedObject(moc, &compactFetchIVarKey, ivars, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[ivars release];
}
return ivars;
}
- (id)init
{
self = [super init];
return self;
}
- (void)dealloc
{
self.lastError = nil;
[super dealloc];
}
@end
A typical class definition, we have @synthesize and init/dealloc. Note, -dealloc silently releases lastError through the nil assignment (via the synthesized property).
What's +fetch: up to? objc_getAssociatedObject retrieves an object previously associated with the target class, our NSManagedObjectContext. If this is the first time through, there is no association and nil is returned. In that case, we must create and associate an instance of our class, CompactFetchIVars, with the context. objc_setAssociatedObject performs the association. Note that both the get & set association calls require a key (in this case, compactFetchIVarKey), and despite the documentation, that key need not be a string. The functions use the key's unique location in program memory (assigned by the compiler) to determine uniqueness of the key.
CompactFetch.m - Category definition
@implementation NSManagedObjectContext (CompactFetch)
- (NSUInteger)defaultFetchLimit
{
return [CompactFetchIVars fetch:self].defaultFetchLimit;
}
- (void)setDefaultFetchLimit:(NSUInteger)limit
{
[CompactFetchIVars fetch:self].defaultFetchLimit = limit;
}
- (NSError*)lastError
{
return [CompactFetchIVars fetch:self].lastError;
}
- (void)setLastError:(NSError*)error
{
[CompactFetchIVars fetch:self].lastError = error;
}
//...
- (NSUInteger)defaultFetchLimit
{
return [CompactFetchIVars fetch:self].defaultFetchLimit;
}
- (void)setDefaultFetchLimit:(NSUInteger)limit
{
[CompactFetchIVars fetch:self].defaultFetchLimit = limit;
}
- (NSError*)lastError
{
return [CompactFetchIVars fetch:self].lastError;
}
- (void)setLastError:(NSError*)error
{
[CompactFetchIVars fetch:self].lastError = error;
}
//...
Finally, we implement the get/set methods for the Category we attached to NSManagedObjectContext. In them, we use the +fetch: class method to retrieve (or create, associate and retrieve) the instance of CompactFetchIVars attached to the NSManagedObjectContext. Then, we get or set the parameter, as appropriate. Under the covers, this goes from a method call on the Core Data instance via the Category through to the attached IVar instance. The runtime knows to call -dealloc on both NSManagedObjectContext and CompactFetchIVars, when retain count goes to zero. DO NOT RETAIN a reference from the IVar class to the attached class; any back track should be assign only, or a retain cycle is created and memory leaks.
Thanks a lot for your help on this blog. I look forward to reading more articles from you!
ReplyDeleteAppium Training in Chennai
Mobile Appium Coaching in Chennai
Appium Training in OMR
JMeter Training Course
JMeter Course
core java training in chennai
C C++ Training in Chennai
javascript training in chennai
I would definitely thank the admin of this blog for sharing this information with us. Waiting for more updates from this blog admin.
ReplyDeleteccna Training center in Chennai
Best CCNA Training Institute in Chennai
ReactJS course
Ethical Hacking Course in Chennai
Best PHP training in chennai
gst classes in chennai
ccna Course in Chennai
web designing training in chennai
I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective. Techno-based information has been fetched in each of your topics. Sure it will enhance and fill the queries of the public needs. Feeling so glad about your article. Thanks…!
ReplyDeletemagento training course in chennai
magento training institute in chennai
magento 2 training in chennai
magento development training
magento 2 course
magento developer training
best php training in chennai
ReplyDeletebest php developer institution chennai
best php training with placement in chennai
best php training center in chennai
best php course in chennai
Innovative blog thanks for sharing this information.
ReplyDeleteSelenium Training in chennai | Selenium Training in annanagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery
Thanks for the informative article About Java. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.good luck.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
its an very nice.
ReplyDeleteRobotic Process Automation (RPA) Training in Chennai | Robotic Process Automation (RPA) Training in anna nagar | Robotic Process Automation (RPA) Training in omr | Robotic Process Automation (RPA) Training in porur | Robotic Process Automation (RPA) Training in tambaram | Robotic Process Automation (RPA) Training in velachery
I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. Good luck for the next!
ReplyDeleteangular js training in chennai
angular js online training in chennai
angular js training in bangalore
angular js training in hyderabad
angular js training in coimbatore
angular js training
angular js online training
Mua vé tại đại lý vé máy bay Aivivu, tham khảo
ReplyDeleteLịch bay từ Hàn Quốc về Việt Nam tháng 7
đặt vé máy bay từ vinh vào sài gòn
giá vé máy bay từ tphcm ra hà nội
vé máy bay đi nha trang giá rẻ
vé máy bay đi đà lạt
bảng giá taxi sân bay nội bài
rastgele görüntülü konuşma - kredi hesaplama - instagram video indir - instagram takipçi satın al - instagram takipçi satın al - tiktok takipçi satın al - instagram takipçi satın al - instagram beğeni satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - instagram beğeni satın al - instagram beğeni satın al - polen filtresi - google haritalara yer ekleme - btcturk güvenilir mi - binance hesap açma - kuşadası kiralık villa - tiktok izlenme satın al - instagram takipçi satın al - sms onay - paribu sahibi - binance sahibi - btcturk sahibi - paribu ne zaman kuruldu - binance ne zaman kuruldu - btcturk ne zaman kuruldu - youtube izlenme satın al - torrent oyun - google haritalara yer ekleme - altyapısız internet - bedava internet - no deposit bonus forex - erkek spor ayakkabı - webturkey.net - karfiltre.com - tiktok jeton hilesi - tiktok beğeni satın al - microsoft word indir - misli indir
ReplyDeleteyoutube abone satın al
ReplyDeletecami avizesi
cami avizeleri
avize cami
no deposit bonus forex 2021
takipçi satın al
takipçi satın al
takipçi satın al
takipcialdim.com/tiktok-takipci-satin-al/
instagram beğeni satın al
instagram beğeni satın al
btcturk
tiktok izlenme satın al
sms onay
youtube izlenme satın al
no deposit bonus forex 2021
tiktok jeton hilesi
tiktok beğeni satın al
binance
takipçi satın al
uc satın al
sms onay
sms onay
tiktok takipçi satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
instagram beğeni satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
takipcialdim.com/instagram-begeni-satin-al/
perde modelleri
instagram takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
betboo
marsbahis
sultanbet
beğeni satın al
ReplyDeleteinstagram takipçi satın al
ucuz takipçi
takipçi satın al
https://takipcikenti.com
https://ucsatinal.org
instagram takipçi satın al
https://perdemodelleri.org
https://yazanadam.com
instagram takipçi satın al
balon perdeler
petek üstü perde
mutfak tül modelleri
kısa perde modelleri
fon perde modelleri
tül perde modelleri
https://atakanmedya.com
https://fatihmedya.com
https://smmpaketleri.com
https://takipcialdim.com
https://yazanadam.com
yasaklı sitelere giriş
aşk kitapları
yabancı şarkılar
sigorta sorgula
https://cozumlec.com
word indir ücretsiz
tiktok jeton hilesi
rastgele görüntülü sohbet
erkek spor ayakkabı
fitness moves
gym workouts
https://marsbahiscasino.org
http://4mcafee.com
http://paydayloansonlineare.com
tiktok jeton hilesi
ReplyDeletetiktok jeton hilesi
binance referans kimliği
gate güvenilir mi
tiktok jeton hilesi
paribu
btcturk
bitcoin nasıl alınır
İnstagram takipçi satın al! İnstagram takipçi sitesi ile takipçi satın al sende sosyal medyada fenomen olmaya bir adım at. Sende hemen instagram takipçi satın almak istiyorsan tıkla:
ReplyDelete1- takipçi satın al
2- takipçi satın al
3- takipçi satın al
iDealshare VideoGo Crack is a multi-purpose, multimedia file format converter. With this app, you can convert video and audio easily. iDealshare VideoGo Crack 2022
ReplyDeleteKaspersky Total Security Crack is a best security app that helps you to protect your multiple devices with multi antivirus protection. Kaspersky Internet Security Activation Code Free
ReplyDeleteHappy Birthday Twin Girls ... Happy Birthday! You two girls came to this world together to make it a better place. May God shower you both with . Twins Birthday Wishes Greeting Card
ReplyDeleteFl Studio Crack includes a graphical user interface based on a pattern-based music sequencer. It supports multiple MIDI inputs and outputs and can be used with a variety of MIDI controllers. It also includes a wide range of built-in virtual instruments, synthesizers, samplers, and effects plugins. Users can also add their own VST and VSTi plugins for additional functionality.
ReplyDeleteDp Animation Maker Crack is a software program that allows users to create animations and graphics. The program is designed for both novice and advanced users, and it offers a range of tools and features to make the animation process easy and efficient.
ReplyDelete