1

1) I am working on one application which contains one single tableview and above that tableview there is one horizontal scrollview which contains number of dynamic button eg.button1,button2....

2)On touch of each button json is comming and i am parsing and showing that json in tableview using NSURLSession.eg.One touch of button1 json will come,on touch of button2 different json will come.

3)After that i want to store each button json array into single array and display accordingly.eg. suppose i clicked on button2 json will come and should be stored in array so that in future i retouch that button data should come from stored array.

I've managed point 1 & 2 but not getting any idea how to solve 3 point. I also added the one image which will give clear understanding about my problem. Please if any one have idea then reply to this question.Thanks in advance. enter image description here

3
  • What you could do is make a collection of arrays. Do you have a special identifier for each button? Then you could store it in a dictionary with the identifier as key. Otherwise it's going to be difficult to identify which array belongs to which button. Commented May 20, 2016 at 6:07
  • @TMob Yes i need special ideantifer for each button. And i want to store each button array into cache for the first time on touch of that button and if i will retouch that according to that button array will come from cache.Can you please give me any example how to store each dynanic button parsed array into dictionary Commented May 20, 2016 at 6:22
  • @Sandeep for identifier you can use the index of array as the button tag. Commented May 20, 2016 at 6:32

2 Answers 2

2

You can use NSCache to cache items if you want, it works like an NSDictionary.

// Init NSCache and declare somewhere as property//you must use singleton approach as well, it's just example i have initialized in viewDidLoad

    - (void)viewDidLoad
    {    
       self.cache = [NSCache alloc] new];
    }

-(void)callApiForKey:(NSString *)buttonName{
        id aObject = [self.cache objectForKey:buttonName]
        if aObject == nil {
          //perform the API call you are talking about
          //after the fetch is preformed and you get data...
          [self.cache setObject: dataObject forKey:buttonName];
          //perform what you need to with said object
        } else {
          //perform operation with cached object
        }
    }

USES:

on your button click action call above method with buton text: yourButton.titleLabel.text.

[self callApiForKey:yourButton.titleLabel.text];

be suer keys must be unique. other wise you may get/set wrong data obviously.

Thanks

Sign up to request clarification or add additional context in comments.

5 Comments

can we add each button array into cache?I tried but getting last touch button array for each cache
@Sandeep i think you are using same key, so the last key update/set is avaible to you
i hope subcategoryid is always different. also how are you initializing NSCache?
"can we add each button array into cache?I tried but getting last touch button array for each cache –" can you elaborate once more what is happenning ? and one more thing is your API response is actually returning different data on every button click ? some time server return same data for test purpose
0
-Each button have a unique tag right? Like button 1 have tag 1, button 2 have tag 2 ,button 3 have tag 3,
-you can manage this thing by Global nsmutabledictionary and nsmutableArray.
-if you click on button 1 before api calling you check 
if(dictionary valueforkey:@"1") (1mean selected button tag)
{
  no need to call api 
  no need to add array in dictionary
  array=[[dictionary valueforkey:@"1"]mutablecopy];(1mean selected button tag)
  [tble reloaddata];

}
else
{
  api calling 
  here you can add array in dictionary
  [dictionary setobject:yourarray forkey:@"1"];
  array=[[dictionary valueforkey:@"1"]mutablecopy];(1mean selected button tag)
  [tble reloaddata];
}

1 Comment

i tried your logic but failed please have a look into my code pastie.org/10845296

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.