我正在尝试为sqlite查询实现NSFastEnumeration协议。发送到释放实例的NSFastEnumeration消息
我遇到:发送到释放实例 消息
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained *)stackbuf count:(NSUInteger)len {
// First call
if(state->state == 0) {
state->mutationsPtr = &state->extra[0];
state->state = 1;
sqlite3_reset(self.statement);
}
state->itemsPtr = stackbuf;
NSUInteger count = 0;
while (count < len) {
int result = sqlite3_step(self.statement);
if (result == SQLITE_DONE) {
break;
}
MyRow *row = [self queryRow];
stackbuf[count] = row;
count += 1;
}
return count;
}
-(MyRow *) queryRow {
MyRow * row = // query sqlite for row
return row;
}
看起来好像在“行”的对象没有被保留下来,所以当它需要在循环中访问其已被释放。
当我在'强'数据集中'countByEnumeratingWithState'中迭代时,是否需要保存结果,以便保留结果?
IE:
@property (nonatomic, strong) NSMutableArray *resultList;
然后while循环中:
while (count < len) {
int result = sqlite3_step(self.statement);
if (result == SQLITE_DONE) {
break;
}
MyRow *row = [self queryRow];
[self.resultList addObject:row]; // throw into a strong array so its retained
stackbuf[count] = row;
count += 1;
}
编辑:
多一点的研究表明,也许我可以只使用__autoreleasing:
MyRow * __autoreleasing row = [self queryRow];
无需维护一组强大的对象。这是正确的解决方案吗?
你不应该命名方法'get ...'。命名约定在ARC中有意义。 – Sulthan
@Sulthan,固定。仍然问题仍然存在...... – lostintranslation