Data comes from the server in JSON, which is placed in a NSDictionary
. Depending on type of requested data the new class object will be created from this NSDictionary
. There're a lot of data comes, so the object holds a reference to NSDictionary
and extracts a value only when referring to a particular variable. Something like lazy initialization:
- (NSString *)imgURL {
if (_imgURL == nil) {
_imgURL = [self makeObjForKey:kImageURL];
}
return _imgURL;
}
This significantly increases application speed, but produces other problems:
- If a value is absent in
NSDictionary
, it remains nil. Then for each subsequent call to this variable there occurs search for it inNSDictionary
. - When copying the entire instance of the class (
NSCopying
protocol), all variables are copied, producing convertion from entireNSDictionary
.
Solutions:
- Put some flag indicating that value has been checked. But then you have to add additional checks
- Only copy
NSDictionary
for object instance, but then later have to parse same variables again
Anyway these solutions are not optimal. Maybe somebody faced with a similar problem and can advise other techniques.