- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在从手动内存管理过渡到 ARC,但遇到了问题。大多数时候,我通过在我的模型类中调用 performSelectorInBackground 来异步执行数据加载。问题是当模型收到 nil(发布)时,我需要停止任何模型代码的执行。在非 arc 中,一切都很简单——只要用户关闭窗口,它的 Controller 就会开始释放自身并释放它的模型 [_myModel release],因此模型停止它的代码执行(数据加载)并调用它的 dealloc 方法.
这在 ARC 中似乎有所不同。即使在从 Controller 收到 nil 消息后,模型仍然执行代码。它的 dealloc 方法仅在其代码执行(数据加载)后被调用。这是一个问题,因为当用户关闭窗口( Controller )时,代码执行应该尽快停止。这是对代码缺乏控制—— Controller 告诉模型——“走开,我不再需要你的工作了”,但模型仍然“正在努力完成它的工作”:)。
假设一个模型执行一些非常繁重的数据处理,持续时间为 10 秒。当用户打开窗口( Controller )时,模型开始进行处理。但是想象一个用户改变主意并关闭窗口,就在打开它之后。该模型仍然执行浪费处理。任何想法如何解决或解决这个问题?我不喜欢在我的模型中有一个特殊的 BOOL“shouldDealloc”属性并在 Controller dealloc 方法中设置为 YES 并在我的模型类条件中使用的想法。有更优雅的解决方案吗?
我做了一些演示项目来展示这个问题。对于测试,只需创建单 View 应用程序并粘贴代码。在 ViewController.xib 文件中创建按钮 - “开始计算” 和 “停止计算”,并将它们的 IBActions 连接到 startCalculationPressed 和 停止计算按下:
ViewController.h
#import "MyModel.h"
@interface ViewController : UIViewController <MyModelDelegate>
- (IBAction)startCalculationPressed:(id)sender;
- (IBAction)stopCalculationPressed:(id)sender;
@end
ViewController.m
@interface ViewController (){
__strong MyModel *_myModel;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)didCalculated
{
NSLog(@"Did calculated...");
}
- (IBAction)startCalculationPressed:(id)sender
{
NSLog(@"Starting to calculate...");
_myModel = nil;
_myModel = [[MyModel alloc] init];
_myModel.delegate = self;
[_myModel calculate];
}
- (IBAction)stopCalculationPressed:(id)sender
{
NSLog(@"Stopping calculation...");
_myModel.delegate = nil;
_myModel = nil;
}
@end
向项目添加新的 MyModel 类:
MyModel.h
@protocol MyModelDelegate <NSObject>
- (void)didCalculated;
@end
@interface MyModel : NSObject
@property (nonatomic, weak) id<MyModelDelegate> delegate;
- (void)calculate;
@end
MyModel.m
@implementation MyModel
- (void)dealloc
{
NSLog(@"MyModel dealloc...");
}
- (void)calculate
{
[self performSelectorInBackground:@selector(performCalculateAsync) withObject:nil];
}
- (void)performCalculateAsync
{
// Performing some longer running task
int i;
int limit = 1000000;
NSMutableArray *myList = [[NSMutableArray alloc] initWithCapacity:limit];
for (i = 0; i < limit; i++) {
[myList addObject:[NSString stringWithFormat:@"Object%d", i]];
}
[self performSelectorOnMainThread:@selector(calculateCallback) withObject:nil waitUntilDone:NO];
}
- (void)calculateCallback
{
[self.delegate didCalculated];
}
@end
UPDATE Martin 是对的,performSelectorOnMainThread 总是保留 self,所以没有办法停止其他线程(ARC 和非 ARC 中)的代码执行,所以释放模型时不会立即调用 dealloc .因此,这应该通过条件检查使用适当的属性(例如委托(delegate))显式完成。
最佳答案
如果一个对象的释放计数下降到零,或者在 ARC 语言中,如果对该对象的最后一个强引用消失了。
[self performSelectorInBackground:@selector(performCalculateAsync) withObject:nil];
添加对 self
的强引用,这解释了为什么对象没有被释放在后台线程完成之前。
没有办法(据我所知)让后台线程“自动”停止。这同样适用于以 dispatch_async()
或 NSOperation
开头的 block 。一旦启动,线程/ block /操作必须在点上监视某些属性保存到哪里停止。
在您的示例中,您可以监控 self.delegate
。如果它变为 nil
,则没有人对结果感兴趣,所以后台线程可以返回。在这种情况下,将 delegate
属性声明为 atomic
是有意义的。
注意 self.delegate
也会自动设置为 nil
即使 stopCalculationPressed
没有被释放(因为它是一个弱属性)被调用。
关于iphone - 圆弧 : sending nil to an object doesn't call its dealloc immediately,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15985617/
我正在阅读一些有关使用 curses 用 c 编写的旧银行柜员管理工具的源代码,这里有一些我无法理解的代码: main(int argc, char *argv[]) { int h1, h2
我已经在 Google Play 商店中启动了应用程序,对于该应用程序,我需要实现立即应用程序更新,以解决已经在使用我的应用程序的问题 我已经尝试过 Github 示例,这些示例是灵活更新而不是立即更
我有简单的启动服务功能并重复他: mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); Pe
我需要一个 EXECUTE IMMEDIATE 将其结果返回到一个表类型(AS TABLE OF)中 如果立即执行返回单个值,我可以使用“INTO somevariable”保存结果 但是,我的立即执
只是想知道这是否是对多列和多行执行立即执行并将其分配给变量的正确方法?我尝试查看示例,但不确定我是否正确连接了这些行? sql_stmt VARCHAR2(200); sql_stmt:
我来自 Eclipse,我习惯于立即报告整个项目的错误。 假设我有这个功能: public void test() { //Do something } 我把它改成 public void te
什么是“立即模式”?给出一个代码示例。 什么时候必须使用立即模式而不是保留模式?使用每种方法的优点和缺点是什么? 最佳答案 “立即模式”的一个示例是使用 glBegin和glEnd与 glVertex
我有一个使用 EXECUTE IMMEDIATE 调用的存储过程。我面临的问题是,直接调用过程与使用 EXECUTE IMMEDIATE 调用过程时的解释计划不同。这导致执行时间增加了 5 倍。计划之
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我有一个表,其中存储了某些条件以及输入参数,如下所示: CONDITION | INPUT_PARAMS ----------------------
我有一个网络应用程序,它创建一些自定义消息并将它们按顺序缓冲在 FIFO 队列中,直到调用 send() 方法,然后所有这些消息都被传输到它们的预期目的地。 还需要提供一种允许“立即发送”功能的方法,
如果存储过程旨在减轻 SQL 注入(inject)攻击,为什么在存储过程中允许 EXECUTE IMMEDIATE?以下问题的公认答案将它们称为针对此类攻击的步骤: What is a stored
我有一个关于“立即执行”的问题。 我在下一个 plsql 语句中动态更改了表名 DECLARE TYPE CurTyp IS REF CURSOR; cur CurTyp; str1 VARCHAR2
最初 immediate标志仅用于 ActionSource界面。但后来它被添加到 EditableValueHolder界面也。设计决策的原因是什么? 最佳答案 它用于对多个 EditableVal
这个问题是关于不允许“创建”权限的环境。 版本是: Oracle 数据库 10g 和 PL/SQL 8.0.0.1480 下面是一个小示例,展示了我正在尝试完成的任务,即循环遍历集合中的一组“字段”并
我有一个使用 EXECUTE IMMEDIATE 的存储过程命令执行一个很长的字符串。如何支持很长的字符串并将数据返回到refcursor? 最佳答案 假设您的 SQL 不超过 32K(正如@Tony
嗨,我真的是 Jsf 和 Primefaces 世界的新手,我有一个关于 Primefaces 轮询组件的简单查询,如下所示。 我有一个登录页面,一旦用户通过身份验证,我将他重定向到welcome.x
在调试断点中,是否可以尝试Dart表达式并执行命令? 就像在Chrome Developer Bar,Firebug或Visual Studio立即窗口中一样。 最佳答案 不幸的是还没有。 标记htt
这个问题已经有答案了: Create user from string variables in a PL/SQL block (1 个回答) 已关闭 6 年前。 我正在 Oracle 数据库上编写
我有以下组件: @Component( label = "Service name", description = "Service description", metatyp
我是一名优秀的程序员,十分优秀!