Q: Understanding a logic in update/add - NSUserDefaults (Objective C) I'm writing a nice class to handle call block in NSUserDefaults.
Haven't tested it yet, but I think I miss something regarding update/add by callBlockID.
My Code:
My Code:
@implementation UserDefaultHandler

+ (UserDefaultHandler *)sharedInstance
{
    static UserDefaultHandler *instance;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[UserDefaultHandler alloc] init];
    });

    return instance;
}

-(void)updateOrAddCallBlock:(NSMutableDictionary*)callBlockObject
{
    NSString *callBlockID = [callBlockObject objectForKey:@"callBlockID"];
    BOOL needToUpdate = NO;
    NSInteger indexDict = 0;
    NSMutableArray *arrayOfAllCallBlock = [[NSMutableArray alloc] init];
    arrayOfAllCallBlock = (NSMutableArray*)[[NSUserDefaults standardUserDefaults] objectForKey:@"arrayOfAllCallBlock"];
    NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];

    if (arrayOfAllCallBlock.count <= 0 || arrayOfAllCallBlock == nil)
    {
        [arrayOfAllCallBlock addObject:callBlockObject];
    }

    else
    {
        for (int i = 0; i < arrayOfAllCallBlock.count; i++)
        {
            NSMutableDictionary *mutableDict = [arrayOfAllCallBlock objectAtIndex:i];
            NSString *idFromDict = [mutableDict objectForKey:@"callBlockID"];

            if ([idFromDict isEqualToString:callBlockID])
            {
                needToUpdate = YES;
                indexDict = i;
                newDict = mutableDict;
            }
        }

        if (needToUpdate)
        {
            [arrayOfAllCallBlock removeObjectAtIndex:indexDict];
            [arrayOfAllCallBlock insertObject:newDict atIndex:indexDict];
        }
        else
        {
            [arrayOfAllCallBlock addObject:callBlockObject];
        }
    }

    [self saveGlobalDict:arrayOfAllCallBlock];
}

- (NSMutableDictionary *)getCallBlockByID:(NSString*)callBlockID
{
    NSMutableArray *arrayOfAllCallBlock = (NSMutableArray*)[[NSUserDefaults standardUserDefaults] objectForKey:@"arrayOfAllCallBlock"];
    BOOL isFound = NO;
    NSInteger indexDict = 0;
    NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];

    for (int i = 0; i < arrayOfAllCallBlock.count; i++)
    {
        NSMutableDictionary *mutableDict = [arrayOfAllCallBlock objectAtIndex:i];
        NSString *idFromDict = [mutableDict objectForKey:@"callBlockID"];

        if ([idFromDict isEqualToString:callBlockID])
        {
            isFound = YES;
            indexDict = i;
            newDict = mutableDict;
        }
    }

    if (isFound)
    {
        return newDict;
    }
    else

    {
        return nil;
    }
}

- (NSMutableArray*)getAllCallBlock
{
    NSMutableArray *arrayOfAllCallBlock = (NSMutableArray*)[[NSUserDefaults standardUserDefaults] objectForKey:@"arrayOfAllCallBlock"];
    return arrayOfAllCallBlock;
}

- (void)clearCallBlockDB
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"arrayOfAllCallBlock"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)saveGlobalDict:(NSMutableArray*)globalArray
{
    self.globalArray = [[NSMutableArray alloc] init];
    self.globalArray = globalArray;
    [self clearCallBlockDB];
    [[NSUserDefaults standardUserDefaults] setObject:self.globalArray forKey:@"arrayOfAllCallBlock"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)clearSingleCallBlock:(NSString*)callBlockID
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    array = [self getAllCallBlock];
    NSMutableDictionary *specificDict = [[NSMutableDictionary alloc] init];
    NSInteger getIndex = [self getIndexForCallBlock:array specificDict:specificDict];
    [array removeObjectAtIndex:getIndex];
    [self saveGlobalDict:array];
}

- (NSInteger)getIndexForCallBlock:(NSMutableArray*)arrayOfAllCallBlock specificDict:(NSMutableDictionary*)specificDict
{
    NSInteger getIndex = 0;
    for (int i = 0; i < arrayOfAllCallBlock.count; i++)
    {
        NSMutableDictionary *mutableDict = [arrayOfAllCallBlock objectAtIndex:i];

        NSDictionary *dict_one = [specificDict copy];
        NSDictionary *dict_two = [mutableDict copy];

        if ([dict_two isEqualToDictionary:dict_one])
        {
            getIndex = i;
        }
    }

    return getIndex;
}

@end

I'll explain: I've a UITableView and when I add a row to it, I've a new window to put details, save and present back in UITableView.
Also, I've an edit button so I can edit and update a row in the table.
Adding a row to table create a unique string (callBlockID).
I'm not sure how to handle it and whether or not my code is enough.
Any ideas?
