1

I load data from json and then add it to nsmutablearray like this:

- (void)loadData
{

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, ^{


            // Create array to hold dictionaries
            myObject = [[NSMutableArray alloc] init];

            NSData *jsonData = [NSData dataWithContentsOfURL:
                                [NSURL URLWithString:@"http://www.domain.com/json.php"]];

            if(jsonData != nil)
            {
                NSError *error = nil;
                id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
                if (error == nil){

                    dispatch_sync(dispatch_get_main_queue(), ^{


                        // values in foreach loop
                        for (NSMutableArray *tempArray in jsonObjects) {

                            [myObject addObject:tempArray];

                            NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"id.doubleValue" ascending:NO];
                            [myObject sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];


                            [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
                            [self performSelectorOnMainThread:@selector(endAnimating) withObject:nil waitUntilDone:YES];
                        }

                    });

                }

            }


    });

}

if I check with NSLog "tempArray" it's looks ok, but if I check "myObject", data added to it multiple times. How to add data just one time in to my "myObject" array?

EDIT:

My JSON result:

    [{"id":"7","title":"monkey","thumb":"http:\/\/icon.s.photosight.ru\/img\/8\/e09\/5045427_thumb.jpg","url":"http:\/\/icon.s.photosight.ru\/img\/8\/e09\/5045427_large.jpg","day":"perjantai","date":"0","likes":"2","device_id":"1111","active":"1"},

{"id":"11","title":"Bukashka","thumb":"http:\/\/icon.s.photosight.ru\/img\/f\/b3b\/5078973_thumb.jpg","url":"http:\/\/icon.s.photosight.ru\/img\/f\/b3b\/5078973_large.jpg","day":"perjantai","date":"0","likes":"1","device_id":"1111","active":"1"},

{"id":"12","title":"blya","thumb":"http:\/\/icon.s.photosight.ru\/img\/f\/c1d\/5076251_thumb.jpg","url":"http:\/\/icon.s.photosight.ru\/img\/f\/c1d\/5076251_large.jpg","day":"perjantai","date":"0","likes":"1","device_id":"1111","active":"1"}]

My NSLog(@"%@", myObject);

2013-06-12 18:45:52.228 testApp[960:60b] (
        {
        active = 1;
        date = 0;
        day = perjantai;
        "device_id" = 1111;
        id = 7;
        likes = 2;
        thumb = "http://icon.s.photosight.ru/img/8/e09/5045427_thumb.jpg";
        title = monkey;
        url = "http://icon.s.photosight.ru/img/8/e09/5045427_large.jpg";
    }
)
2013-06-12 18:45:52.230 testApp[960:60b] (
        {
        active = 1;
        date = 0;
        day = perjantai;
        "device_id" = 1111;
        id = 11;
        likes = 1;
        thumb = "http://icon.s.photosight.ru/img/f/b3b/5078973_thumb.jpg";
        title = Bukashka;
        url = "http://icon.s.photosight.ru/img/f/b3b/5078973_large.jpg";
    },
        {
        active = 1;
        date = 0;
        day = perjantai;
        "device_id" = 1111;
        id = 7;
        likes = 2;
        thumb = "http://icon.s.photosight.ru/img/8/e09/5045427_thumb.jpg";
        title = monkey;
        url = "http://icon.s.photosight.ru/img/8/e09/5045427_large.jpg";
    }
)
2013-06-12 18:45:52.237 testApp[960:60b] (
        {
        active = 1;
        date = 0;
        day = perjantai;
        "device_id" = 1111;
        id = 12;
        likes = 1;
        thumb = "http://icon.s.photosight.ru/img/f/c1d/5076251_thumb.jpg";
        title = blya;
        url = "http://icon.s.photosight.ru/img/f/c1d/5076251_large.jpg";
    },
        {
        active = 1;
        date = 0;
        day = perjantai;
        "device_id" = 1111;
        id = 11;
        likes = 1;
        thumb = "http://icon.s.photosight.ru/img/f/b3b/5078973_thumb.jpg";
        title = Bukashka;
        url = "http://icon.s.photosight.ru/img/f/b3b/5078973_large.jpg";
    },
        {
        active = 1;
        date = 0;
        day = perjantai;
        "device_id" = 1111;
        id = 7;
        likes = 2;
        thumb = "http://icon.s.photosight.ru/img/8/e09/5045427_thumb.jpg";
        title = monkey;
        url = "http://icon.s.photosight.ru/img/8/e09/5045427_large.jpg";
    }
)

WORKING SOLUTION BY: danypata

in viewDidLoad put myObject = [[NSMutableArray alloc] init];

then

- (void)loadData
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, ^{
            NSData *jsonData = [NSData dataWithContentsOfURL:
                                [NSURL URLWithString:@"http://www.domain.com/json.php"]];
            if(jsonData != nil)
            {
                NSError *error = nil;
                id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
                if (error == nil){

                    [myObject removeAllObjects];
                    for (NSMutableDictionary *tempDict in jsonObjects) {

                        [myObject addObject:tempDict];
                    }


                    NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"id.doubleValue" ascending:NO];
                    [myObject sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];

                    dispatch_sync(dispatch_get_main_queue(), ^{
                        [self.tableView reloadData];
                        [self.tableView.pullToRefreshView stopAnimating];
                    });
                }
            }

        });

}

1 Answer 1

2

One possible cause of your problem is that the tempArray arrays contains same objects, but to reply to your question `how to add just one time in to my "myObject" array" there are two easy solutions

One, use containsObject: method like this:

 if([myObject containsObject:tempArray] == NO) {
    [myObject addObject:tempArray]
 }

Second, which I think is more elegant use NSMutableSet (`NSMutableSet adds objects only if the object is not already added). You can use it like this:

 NSMutableSet *set = [[NSMutableSet alloc] init];
 [set addObject:tempArray];
 //after you added all objects just do the following after you init your myObject
 [myObject addObjectsFromArray:[set allObjects]]

EDIT

Your problem is caused by the for loop. You are nto extracting properly the data, in your JSON you have an array of dictionaries not an array of arrays so you should change the for loop like this:

for (NSDictionary *tempDict in jsonObjects) {
   [myObject addObject:tempDict];
   //other operations here 
}
9
  • I tried first one and it doesn't work for me. If I put this if([myObject containsObject:jsonObjects] == NO) { [myObject addObject:tempArray]; NSLog(@"test"); } It gives me 3 times "test". Commented Jun 12, 2013 at 15:09
  • @PavelKaljunen Form what I see in your question, don't check if the myObject contains jsonObjects, check if contains tempArray. Your myObject will nenver contain jsonObjects because you never add it to the array
    – danypata
    Commented Jun 12, 2013 at 15:17
  • Oh sorry, I mean like this: if([myObject containsObject:tempArray] == NO) { [myObject addObject:tempArray]; } Commented Jun 12, 2013 at 15:18
  • I have 3 objects in my json result and it add 3 times this 3 objects in to my array Commented Jun 12, 2013 at 15:20
  • @PavelKaljunen can you post your JSON, I think it will be faster to find a solution :)
    – danypata
    Commented Jun 12, 2013 at 15:21

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.