gpt4 book ai didi

iOS 后台进程和 AFNetworking

转载 作者:行者123 更新时间:2023-11-28 20:08:51 31 4
gpt4 key购买 nike

我有一个从网络服务加载一些数据的应用程序。一切都很好。

现在我想在后台进程中更新数据。编写了该部分,我可以通过在 Xcode 中使用“模拟后台获取”调试过程来启动它。
我的问题是 AFHTTPRequestOperation 中的 block 没有被执行。我敢打赌我不想要这里的 block ,但是
a) 我想知道为什么它不会执行 - 我的猜测是你不能在后台提取中像这样分离 block 。
b) 我不知道如何在这里不使用 block 。

如有任何帮助,我们将不胜感激。

    - (void) callWebServiceUpdate {

//Delete tmpDatabase if it is there
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success;
success = [fileManager fileExistsAtPath:self.tmpEmployeeDatabasePath];
if (success) {
[fileManager removeItemAtPath:self.tmpEmployeeDatabasePath error:nil];
}

//Write data to temp database
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"USER" password:@"PASSWORD"];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

AFHTTPRequestOperation *operationNow = [manager GET: @"https://xxxxxxxxx/mobile/mobilede.nsf/restServices.xsp/LotusDirectoryPeople"
parameters: [self jsonDict]
success: ^(AFHTTPRequestOperation *operation, id responseObject)

{
NSMutableArray *employees = (NSMutableArray *)responseObject;
FMDatabase *db = [FMDatabase databaseWithPath:self.tmpEmployeeDatabaseName];
[db open];
for(NSDictionary *dict in employees)
{
BOOL success = [db executeUpdate:@"INSERT INTO tmpemployees (id,firstName,lastName,fullName,email) VALUES (?,?,?,?,?);",
[dict objectForKey:@"clockNumber"],
[dict objectForKey:@"firstName"],
[dict objectForKey:@"lastName"],
[dict objectForKey:@"fullName"],
[dict objectForKey:@"email"],
nil];
if (success) {} // Only to remove success error
}
}

failure:^(AFHTTPRequestOperation *operation, NSError *error)
{NSLog(@"Error: %@", error);}
];
[operationNow start];

//Loop through the tmp db and see if we have a value to update or add
//id tmpDatabasePath = [(AppDelegate *)[[UIApplication sharedApplication] delegate] databasePath];
NSMutableArray *tmpEmployees;
tmpEmployees = [[NSMutableArray alloc] init];

FMDatabase *tmpDatabase = [FMDatabase databaseWithPath:self.tmpEmployeeDatabasePath];
[tmpDatabase open];

//id realDatabasePath = [(AppDelegate *)[[UIApplication sharedApplication] delegate] databasePath];
FMDatabase *realDatabase = [FMDatabase databaseWithPath:self.employeeDatabasePath];
[realDatabase open];


FMResultSet *results = [tmpDatabase executeQuery:@"SELECT * FROM employees"];
while([results next])
{
employee *thisEmployee = [employee new];
thisEmployee.firstName = [results stringForColumn:@"firstName"];
thisEmployee.lastName = [results stringForColumn:@"lastName"];
thisEmployee.fullName = [results stringForColumn:@"fullname"];
thisEmployee.city = [results stringForColumn:@"city"];
thisEmployee.ste = [results stringForColumn:@"state"];
thisEmployee.emailAddress = [results stringForColumn:@"email"];

NSString *tst = [results stringForColumn:@"id"];
thisEmployee.id = [NSNumber numberWithInt:[tst intValue]];

//See if we have a record
FMResultSet *results = [realDatabase executeQuery:@"SELECT * from db where id= ?",thisEmployee.id];




}
[tmpDatabase close];
}

======================================

下面的海报是正确的。我添加了这段代码:

NSURLSessionConfiguration *backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"background"];
AFURLSessionManager *backgroundManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:backgroundConfiguration];

但是,我不明白如何将我的 JSON URL 请求添加到 AFURLSessionManager。

最佳答案

我不知道 AFNetworking 的内部结构,但后台获取需要使用 NSURLSessionNSURLSessionDownloadTask 对象来完成。检查您调用的函数是否在内部使用适当的类 - 在正常操作中工作正常的方法可能只是拒绝在后台获取模式下工作。

在简要查看了 AFNetworking 文档之后,我认为您可能需要设置一个 AFURLSessionManager 的实例来配置后台获取。

关于iOS 后台进程和 AFNetworking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21032751/

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