mardi 16 juin 2015

Takes more time in insertion of images in the database, what to do?

I am new in ios, in my product cataloge app I am getting image links from json and after parsing it i am converted it into nsdata and inserting it into sqlite, problem is that it takes more time than android app for inserting image into database, and i also have a doubt it takes longer than normal time to inserting is there any wrong with my code, please help.

this is the first method to parse images and orther text data

-(void)jsonparseimage:(NSString *)jsonstring
{
    NSError *error;
    SBJSON *json = [SBJSON new];
    NSDictionary *statusDict=[json objectWithString:jsonstring error:&error ];
    NSArray *feed= [statusDict objectForKey:@"CATEGORY_DETAILS"];

    if([[statusDict objectForKey:@"CATEGORY_DETAILS" ]count ]>0)
    {
        [appdelegate.categoryarray removeAllObjects];

        for (NSDictionary *dictionaryValue in feed)
        {

            for (NSString *valueKey in [dictionaryValue allKeys])
            {

                CategoryData *catData=[[CategoryData alloc]init];
                catData.categoryname=valueKey;
                [appdelegate.categoryarray addObject:valueKey];
                NSArray *catInfo =[dictionaryValue objectForKey:valueKey];

                NSMutableArray *newArray=[[NSMutableArray alloc]init];
                for (NSMutableDictionary *categoryData in catInfo)
                {
                    imageconvert *convert=[[imageconvert alloc]init];
                    convert.thumbimage=[categoryData objectForKey:@"thumb"];
                    convert.mainimage=[categoryData objectForKey:@"mainimg"];
                    convert.categaryname=[categoryData objectForKey:@"cat_nm"];
                    convert.imageName=[categoryData objectForKey:@"image_nm"];
                    convert.lastmodified=[categoryData objectForKey:@"lastmodified"];
                    [newArray addObject:convert];
                }
                catData.imageArraya=newArray;
                NSLog(@"all Dataaaaa :%d  %d  %@",[catData.imageArraya count],[alldata count],catData.categoryname);
                [alldata addObject:catData];
            }
        }

    }

    [self.tableView reloadData];

    [self insertintoDatabase:alldata];

}

this is second to fetch imagedata in nsdata from link

-(void)insertintoDatabase:(NSMutableArray *)allCatData
{

    for (int i=0;i<[allCatData count] ;i++)
    {
        CategoryData *catData=[allCatData objectAtIndex:i];
        appdelegate.categoryString=catData.categoryname;

        NSLog(@"array size :%d",[catData.imageArraya count]);

        for (int j=0; j<[catData.imageArraya count]; j++)
        {
            imageconvert *convertedData=[catData.imageArraya objectAtIndex:j];
            NSError* error = nil;

            NSData* thumbData = [NSData dataWithContentsOfURL: [NSURL URLWithString:[convertedData.thumbimage stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] options:NSDataReadingUncached error:&error];


            if (error)
            {
                NSLog(@"%@", [error localizedDescription]);
            }
            else
            {
                [self SaveThumbDatatosql:thumbData WithCatName:catData.categoryname withImagename:convertedData.imageName withLastmodified:convertedData.lastmodified];
            }
        }
    }
    [blurView removeFromSuperview];
    [self viewDidLoad];
}

this is method for insertion

- (void) SaveThumbDatatosql:(NSData*) thumbData WithCatName:(NSString*) CategoryName withImagename:(NSString*) ImageName withLastmodified:(NSString*) Lastmodified
{
    //    NSLog(@"thumbData:%@ imageData:%@ CategoryName:%@ ImageName:%@ Lastmodified:%@",thumbData,imageData,CategoryName,ImageName,Lastmodified);

    sqlite3 *database;
    if (sqlite3_open([appdelegate.databasePath UTF8String], &database) == SQLITE_OK)
    {
        sqlite3_stmt *statement;

        const char* sqliteQuery = "INSERT INTO CategoryTable (ThumbImagePath,Categoryname,Imagename,Lastmodified) VALUES (?,?,?,?)";

        if (sqlite3_prepare_v2(database, sqliteQuery, -1, &statement, NULL) == SQLITE_OK)
        {
            sqlite3_bind_blob(statement, 1, [thumbData bytes], [thumbData length], SQLITE_STATIC);
            sqlite3_bind_text(statement, 2, [CategoryName UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 3, [ImageName UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 4, [Lastmodified UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_step(statement);
            sqlite3_finalize(statement);
            NSLog(@"inserted ");
        }
        else
        {
            NSAssert1(0, @"error:'%s'", sqlite3_errmsg(database));
            // NSLog(@"not inserted ");
        }
        sqlite3_close(database);
    }

}

Aucun commentaire:

Enregistrer un commentaire