gpt4 book ai didi

iphone - NSBlockOperation 在 NSOperation 中调用一个方法

转载 作者:行者123 更新时间:2023-11-29 11:00:39 27 4
gpt4 key购买 nike

我有一个问题。我有以下代码:

NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock:^{

[[ClassA sharedInstance] someSingletonMethod:params1];
[ClassB classBMethod:params2];
[self currentClassMethod:params3];

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"kSNotificationName" object:nil];
}];
}];

[self.myOperationQueue addOperation:op];

在 block 中调用单例方法安全吗?在 block 中调用类方法是否安全?调用“self”方法安全吗?

我有以下情况。我正在向服务器发送一批请求:

AFHTTPClient *client=[[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client enqueueBatchOfHTTPRequestOperations:reqOps progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"finished: %i of %i requests", numberOfFinishedOperations, totalNumberOfOperations);
[[PTDictionaryUpdate sharedInstance] debugPrint:[NSString stringWithFormat:@"finished: %i of %i requests", numberOfFinishedOperations, totalNumberOfOperations]];
} completionBlock:^(NSArray *operations) {
NSLog(@"operations finished");

这是我处理响应的方式。我正在创建操作来处理已完成的请求。

for (int i=0; i<[operations count]; i++)
{
AFJSONRequestOperation *operation=[operations objectAtIndex:i];
if ((operation.error==nil) && (operation.response.statusCode==200))
{
id JSON=operation.responseJSON;
int handleMethodIndex=-1;
for (int j=0; j<[urls count]; j++)
{
if ([operation.request.URL isEqual:[urls objectAtIndex:j]])
{
handleMethodIndex=j;
};
};

switch (handleMethodIndex) {
case 0:
{
//[self countryUpdate:JSON];

NSInvocationOperation *invOp=[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(countryUpdate:) object:JSON];
[invOp setQueuePriority:NSOperationQueuePriorityLow];
[handleJSONOperations addObject:invOp];
break;
}
case 1:
{
//[self regionsUpdate:JSON];

NSInvocationOperation *invOp=[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(regionsUpdate:) object:JSON];
[invOp setQueuePriority:NSOperationQueuePriorityLow];
[handleJSONOperations addObject:invOp];
break;
}
//.......
//.......
}

在我创建了一个数组,其中包含处理(处理和更新数据库)我从服务器提取的 JSON 的操作:

NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock:^{

//first we need to tether countries, regions and cities
[[PTDataTetherer sharedInstance] tetherCountriesRegionsCitiesInContext:self.updateContext];

//generating fake agencies
//[PTFakeAgencyGenerator generateAgenciesInContext:context];

//generating fake clients
//[PTFakeClientGenerator generateClientsInContext:context];

//generating fake reports
[[PTFakeReportGenerator sharedInstance] generateReportsInContext:self.updateContext];

//generating fake presentations
[[PTFakePresentationGenerator sharedInstance] generatePresentationsInContext:self.updateContext];


//tethering
[[PTDataTetherer sharedInstance] tetherAgenciesWithOthersInContext:self.updateContext];
[[PTDataTetherer sharedInstance] tetherClientsWithOthersInContext:self.updateContext];
[[PTDataTetherer sharedInstance] tetherEventsWithOthersInContext:self.updateContext];
[[PTDataTetherer sharedInstance] tetherPresentationFoldersWithImagesInContext:self.updateContext];

[self saveContext];

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"kSynchronizationFinishedNotification" object:nil];
}];
}];
[op setQueuePriority:NSOperationQueuePriorityLow];
if ([handleJSONOperations count]==0)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"kSynchronizationFinishedNotification" object:nil];
}];
}
else
{
[self.serverUpdateQueue addOperation:updateContextCreateOperation];
[handleJSONOperations addObject:op];
[self.serverUpdateQueue addOperations:handleJSONOperations waitUntilFinished:NO];
};

基本上我想以这种方式构建队列:1.【上下文创建操作】2. [将解析从服务器接收的 json 并将新/修改对象保存到/在上下文中的多个上下文修改操作]3. [一些最后的方法也会修改上下文,最后会调用保存方法将更改传播到存储,然后使用 NSManagedObjectContextDidSaveNotifications 到其他上下文]

最佳答案

Is it safe to call singletons methods in block?

这是一个有点板的问题,取决于你在单例方法中的内容。

Is it safe to call class methods in block?

取决于您在方法中执行的操作。根据我的经验和我所做的代码,是的。

Is it save to call "self" methods?

您正在将 self 的引用传递给该 block ,这可能会导致内存泄漏。

关于iphone - NSBlockOperation 在 NSOperation 中调用一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16034846/

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