gpt4 book ai didi

ios - ARC 在使用 block 时不释放内存

转载 作者:行者123 更新时间:2023-11-28 20:04:05 25 4
gpt4 key购买 nike

我在使用 ARC 时遇到了问题。我所做的是同步:我从 Web 服务请求数据并将其写入数据库(使用 fmdb)。

这是我的完整代码

dispatch_async(queue, ^{

hud.labelText = [NSString stringWithFormat:@"Sincronizzo le aziende"];
[Model syncAziende:^(id response, NSError *error) {
hud.progress += offset;
dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

hud.labelText = [NSString stringWithFormat:@"Sincronizzo i contatti"];
[Model syncContatti:^(id response, NSError *error) {
hud.progress += offset;
dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

....

+ (void)syncAziende:(RequestFinishBlock)completation
{
[self syncData:^(id response, NSError *error) {
completation(response,error);
} wsEndPoint:kCDCEndPointGetAziende tableName:kCDCDBAziendeTableName];
}

+ (void)syncData:(RequestFinishBlock)completation wsEndPoint:(NSString*) url tableName:(NSString *)table
{
NSLog(@"%@",url);
[self getDataFromWS:^(id WSresponse, NSError* WSError)
{
if (!WSError)
[self writeDatatoDB:^(id DBresponse,NSError* DBError)
{
completation(DBresponse,DBError);
}table:table shouldDeleteTableBeforeUpdate:YES data:WSresponse];
else
completation(nil,WSError);
WSresponse = nil;
}WSUrl:url];
}

+ (void)getDataFromWS:(RequestFinishBlock)completation WSUrl:(NSString *)svcUrl
{
[self getJsonDataFromURL:^(id response, NSError *error)
{
completation(response,error);
}url:svcUrl];
}

+(void)getJsonDataFromURL:(RequestFinishBlock)completation url:(NSString*)url
{
AFHTTPRequestOperationManager *manager = [self getAuthorizedRequestionOperationManager];

if (manager) { //OK I'have internet connection
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];

[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
completation([responseObject objectForKey:@"d"],nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completation(nil,error);
}];
}
else //ERROR: I don't have internet connection
{
NSDictionary *dError = [[NSDictionary alloc] initWithObjectsAndKeys:kCDCErrorNoInternetConnectionStatusMessage,@"error", nil];
NSError *error = [[NSError alloc]initWithDomain:url code:kCDCErrorNoInternetConnectionStatusCode userInfo:dError];
completation(nil,error);
}
}


+ (void) writeDatatoDB:(RequestFinishBlock)completion
table:(NSString *)tableName
shouldDeleteTableBeforeUpdate:(BOOL)deleteTable
data:(NSMutableArray *)data
{
NSLog(@"Inizio le operazioni sul database");
__block int errors = 0;

classAppDelegate *appDelegate = (classAppDelegate *)[[UIApplication sharedApplication]delegate];
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:appDelegate.dbFilePath];
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {

if (deleteTable)
[db executeUpdate:[NSString stringWithFormat:@"DELETE FROM %@", tableName]];

for (NSDictionary *jString in data)
{
NSMutableArray* cols = [[NSMutableArray alloc] init];
NSMutableArray* vals = [[NSMutableArray alloc] init];

for (id currentValue in jString)
{
if (![currentValue isEqualToString:@"__metadata"]) {
[cols addObject:currentValue];
[vals addObject:[jString valueForKey:currentValue]];
}
}

NSMutableArray* newCols = [[NSMutableArray alloc] init];
NSMutableArray* newVals = [[NSMutableArray alloc] init];
NSString *value = @"";

for (int i = 0; i<[cols count]; i++) {
@try {
NSString *element = [vals objectAtIndex:i];
if (![element isKindOfClass:[NSNull class]]) {
value = [element stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
[newCols addObject:[NSString stringWithFormat:@"'%@'", [cols objectAtIndex:i]]];
[newVals addObject:[NSString stringWithFormat:@"'%@'", value]];
}
}
@catch (NSException *exception) {

}
}

NSString* sql = [NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (%@)",tableName, [newCols componentsJoinedByString:@", "], [newVals componentsJoinedByString:@", "]];
[db executeUpdate:sql];

if([db lastErrorCode] == 1) //ERRORE!!
{
errors++;
}
}
completion(nil,nil);


NSLog(@"Ho completato le operazioni sul database con %i errori",errors);
}];
}

我从网络服务获得的数据约为 75mb,但在 Xcode 中我看到内存达到 500mb,这导致 iPad 2 崩溃。

最佳答案

你肯定在你的 block 中做了一个保留周期

它主要发生在你在 block 中调用 self 时。因此 self 保留在 block 和主序列中。所以两者相互依存,ARC 认为两者都被对方需要。

你应该使用 weak self 或其他类似的方法。

这里有一些帮助:The Correct Way to Avoid Capturing Self in Blocks With ARC

关于ios - ARC 在使用 block 时不释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22714502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com