【发布时间】:2014-09-28 02:07:44
【问题描述】:
在一个按钮上点击大约 60 多次我的 iPad 应用程序变慢,然后最终崩溃
在启动时,我的应用程序运行良好,一段时间后它变得越来越慢......
响应动作大约需要8到10秒,最后崩溃 我不知道为什么这一切会发生。 我在 uiviewcontroller 上使用了一个集合视图,它的所有内容视图都是在自定义单元类中创建的。
使用 Instruments 在 iPad 2 上进行测试后,拍摄了这些屏幕截图。
现在我该怎么办.??你看到这里有什么问题吗...???
这是 UICollectionView 单元格中的加号按钮,用于执行操作,按钮点击时间随机增加,达到 300000+ 毫秒
注意--->主线程显示0.0毫秒
这是什么意思....????
请指引我正确的方向
编辑
这是我在点击按钮时在这些方法中所做的代码
// 'purchasedProduct' is NSMutuableDictionary
-(void)btnPlus:(id)sender event:(id)event
{
indexPaths = [[NSMutableArray alloc]init];
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:myCollection];
btnIndex = [myCollection indexPathForItemAtPoint: currentTouchPosition];
MyCell *cell = (MyCell*)[myCollection cellForItemAtIndexPath:btnIndex];
NSString *newCode = [productID objectAtIndex:btnIndex.row];
newQty = cell.cellQty.text;
newQty = [NSString stringWithFormat:@"%d",[newQty integerValue] + 1];
BOOL currentCellHasUpdates = purchasedProduct[newCode] != nil;
if (currentCellHasUpdates)
{
// Object Already Exist.......
[purchasedProduct setObject:newQty forKey:newCode];
prdID = newCode;
[self UpdateProduct];
}
else
{
// create new object........
[purchasedProduct setObject:newQty forKey:newCode];
prdID = newCode;
[self BuyProduct];
}
[indexPaths addObject:btnIndex];
[myCollection reloadItemsAtIndexPaths:indexPaths]; // I think problem is here.
}
CellForItemAtIndexPath 方法是
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];
[[cell cellBtnPlus] addTarget:self action:@selector( btnPlus:event:) forControlEvents:UIControlEventTouchUpInside];
[[cell cellBtnMinus] addTarget:self action:@selector(btnMinus:event:) forControlEvents:UIControlEventTouchUpInside];
BOOL currentCellHasUpdates = purchasedProduct[[productID objectAtIndex:indexPath.row]] != nil;
if (currentCellHasUpdates)
{
cell.cellQty.text = [purchasedProduct objectForKey: [productID objectAtIndex:indexPath.row]];
cell.cellQty.textColor = [UIColor blueColor];
}
else
{
cell.cellQty.textColor = [UIColor lightGrayColor];
cell.cellQty.text = @"0";
}
image = nil;
NSString *imageName =[[productID objectAtIndex:indexPath.row] stringByAppendingString:@".jpg"];
getImagePath = [documentsDirectory stringByAppendingPathComponent:imageName];
image = [UIImage imageWithData:[NSData dataWithContentsOfFile:getImagePath]];
if (image.size.width == image.size.height)
{
//set newSize
}
if (image.size.width < image.size.height)
{
//calculate newSize
}
else
{
//calculate newSize
}
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (image.size.width == image.size.height)
{
//set imgRect
}
if (image.size.width < image.size.height)
{
//Calculations for imgRect
}
if (image.size.width > image.size.height)
{
//Calculations for imgRect
}
cell.myImageView.frame = imgRect;
cell.myImageView.image = image;
cell.lbl1.text = [[desc objectAtIndex:indexPath.row] capitalizedString];
cell.lbl2.text = [@"Box Qty:" stringByAppendingString:[boxQty objectAtIndex:indexPath.row]];
cell.lbl4.text = [[code objectAtIndex:indexPath.row] uppercaseString];
cell.lbl5.text = [@"Pack Qty:" stringByAppendingString:[packQty objectAtIndex:indexPath.row]];
BOOL isStarProduct = starProducts[[productID objectAtIndex:indexPath.row]] != nil;
if (isStarProduct)
{
cell.lbl6.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"star.png"]];
}
else
{
cell.lbl6.backgroundColor = [UIColor clearColor];
}
return cell;
}
我认为问题在于 CollectionView 中特定索引路径上的重新加载 导致问题,因为它需要 91% 的时间来执行。我说的对吗..???
编辑 2
-(void)BuyProduct
{
newDate = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
currentDate = [formatter stringFromDate:newDate];
database = [dBName UTF8String];
if (sqlite3_open(database, &dbConnection) == SQLITE_OK)
{
sqliteQuery = [NSString stringWithFormat:@"INSERT INTO ShopingCart"
" (fld1 , fld2, fld3, fld4, fld5, fld6, fld7, fld8, fld9, fld10, fld11, fld12, fld13, fld14, fld15, fld16)"
" VALUES (\'%d\', \"%@\", \'%d\', \"%@\", \"%@\", \"%@\", \"%f\", \"%f\", \"%@\", \"%@\","
" \"%@\", \"%f\", \"%.4f\", \'%d\', \'%d\', \'%d\')",
1,
currentDate,
91,
customerID,
[productID objectAtIndex:btnIndex.row],
[costPrice objectAtIndex:btnIndex.row],
[[price objectAtIndex:btnIndex.row] floatValue] * [newQty integerValue],
[[salePrice objectAtIndex:btnIndex.row] floatValue] * [newQty integerValue],
[qty objectAtIndex:btnIndex.row],
newQty,
[taxRate objectAtIndex:btnIndex.row],
[[price objectAtIndex:btnIndex.row] floatValue] * [newQty integerValue],
[[price objectAtIndex:btnIndex.row] floatValue] * [newQty integerValue] + [taxAmount floatValue],
0,
0,
0];
if (sqlite3_prepare_v2(dbConnection, [sqliteQuery UTF8String], -1, &sQLStatement, NULL) == SQLITE_OK)
{
if (sqlite3_step(sQLStatement) == SQLITE_DONE)
{
NSLog(@"Inserted into Shopping Cart");
}
sqlite3_finalize(sQLStatement);
}
sqliteQuery = [NSString stringWithFormat:@"SELECT SUM(NetPrice) As TotalAmount"
" FROM ShopingCart WHERE CustomerID = \'%@\'",customerID];
if (sqlite3_prepare_v2(dbConnection, [sqliteQuery UTF8String], -1, &sQLStatement, NULL) == SQLITE_OK)
{
if (sqlite3_step(sQLStatement) == SQLITE_ROW)
{
temp = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(sQLStatement, 0)];
}
sqlite3_finalize(sQLStatement);
}
ordAmount.text = [NSString stringWithFormat:@"Total £ %.2f",[temp floatValue]];
}
else
{
NSLog(@"Error %s ", sqlite3_errmsg(dbConnection));
}
sqlite3_close(dbConnection);
}
我应该/可以在哪里改进我的代码?
【问题讨论】:
-
您的问题是 cellForRow 方法。包括该代码。 Instruments 明确强调它是主要问题
-
@DanielGalasko 你的意思是 cellForItemAtIndexPath 方法??
-
@DanielGalasko 请检查此方法。我已经更新了帖子
-
好的,我正在发布答案,不会太久
-
完成,如果有帮助请告诉我
标签: ios iphone ipad profiling instruments