0

I am calling an Api asynchronously to get the data. Then I am assigning the data to a shared instance successfully inside a block given below.

RestApiManager *api= [[RestApiManager alloc]init];
[ObjectBuilder sharedManager];

self.obj = [[Object alloc]init];
[api get_data:credential.password finish:^(NSDictionary *data) {

    if(data){
       self.obj = [ObjectBuilder obj:data];
        [[ObjectBuilder sharedManager]setObject:self.obj];

    }

This all working as expected except when I try to update some of the values in one of NSMutableDictionary properties of the obj.

- (void)update: (NSInteger) temp {

   array3 = [[NSMutableArray alloc]init];
   NSNumber *start =[[NSNumber numberWithInteger:var];
   [NSNumber numberWithInteger:var2];
   NSNumber *temp2 =[NSNumber numberWithInteger:temp];
   [array3 addObject:temp2];
   [array3 addObject:start];
   [array3 addObject:end];
   [self.obj.sch setObject:array3 forKey:temp2];

}

[obj.sch setObject:array3 forKey:temp2]; always crashes my app.

Now I have vague idea whats going on, basically I am setting obj inside a block code, whilst in my update function I am trying to change the contents outside block code.

The error I get is

reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance

Note this error goes away if i declare line

[[ObjectBuilder sharedManager]setObject:obj];

Outside the finish:^ block But then I lose all the data obtained from api call.

Object is declared as below @interface Object : NSObject @property (strong, nonatomic) NSMutableDictionary *sch;

humble_pie
  • 119
  • 10
  • 1
    And what's the error message? What I don't understand is what is `obj` on that line? Because in the code before, it's a local var. – Larme Nov 16 '18 at 12:56
  • @Larme I updated the code, and error I am getting and also added the line which explains when the error disappears, but I lose the data. – humble_pie Nov 16 '18 at 14:15
  • 2
    `__NSDictionaryI` means `NSImmutableDictionary` (that's why there is an uppercase "i" at the end), in other words, a `NSDictionary`, not a `NSMutableDictionary`. – Larme Nov 16 '18 at 14:26
  • I have added the declaration of sch in Object model. Its declared as mutabledictionary. But odd thing is why this error only appears when sharedManager is declared inside the block. – humble_pie Nov 16 '18 at 14:34
  • Declaring and setting is different. For instance if you do `sch = someDictionaryImmutable`, you'll get the same issue. – Larme Nov 16 '18 at 14:37
  • so whats the fix here – humble_pie Nov 16 '18 at 15:02
  • `sch = [NSMutableDictionary dictionaryWithDictionary: someDictionaryImmutable];` instead? – Larme Nov 16 '18 at 15:03
  • NSDictionary *dict = [[NSDictionary alloc]init]; [dict setValue:array3 forKey:temp2]; [self.obj.sch setDictionary:dict]; This resulted in same error – humble_pie Nov 16 '18 at 15:15
  • `sch` is supposed to be a mutable dictionary. So assign it an `NSMutableDictionary`, not an `NSDictionary`. – rmaddy Nov 16 '18 at 16:30
  • @rmaddy this seem to work self.obj.sch = [NSMutableDictionary dictionaryWithObject:array3 forKey:temp2]; But I now face a newer issues, since I run this in a loop, where array3 gets new values on each iteration, I only get final values into my sch. ideally I like a way, it saves array3 and key and adds second array3 and key separately instead of overwriting – humble_pie Nov 16 '18 at 16:39
  • 1
    If you have a new issue, post a new question with clear and specific details. And you may as well delete this question. – rmaddy Nov 16 '18 at 16:41
  • Retain cycle ahead! Never reference `self` strongly inside of a block. It shouldn't crash your app though... `__block typeof(self) weakSelf = self` and then use `weakSelf.obj = ` – Julian F. Weinert Nov 16 '18 at 17:03
  • Check this answer for more detail https://stackoverflow.com/a/20032131/1041122 – Julian F. Weinert Nov 16 '18 at 17:08
  • @JulianF.Weinert it didn't work – humble_pie Nov 16 '18 at 18:02
  • Ok, let me check back later. But nevertheless, go read that reply I linked, it's really good info. I love working with blocks and fell for this trap for quite some time ;) – Julian F. Weinert Nov 16 '18 at 18:22
  • 1
    My comment wasn't actaually an attenpt to fix your issue, only additional information. Please follow rmaddys advice and create a new question for the new issue. – Julian F. Weinert Nov 17 '18 at 18:38

0 Answers0