问题描述
我在iOS应用程序中使用 FMDatabaseQueue
。我一直在理解如何在创建队列时返回值。感谢您的帮助!!
I'm using FMDatabaseQueue
in my iOS application. I'm stuck in understanding how to return the value upon creating the queue. Appreciate your help!!
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath];
[queue inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
FMResultSet *rs = [db executeQuery:@"select * from foo"];
while ([rs next]) {
...
}
// I want value to be returned from here
}];
推荐答案
这取决于你想要返回的内容。但是你可能会感到困惑的是,如果你从 inDatabase
块中发出 return
语句,你就会返回从块中,您不会从包含此 inDatabase
块的方法返回。
It depends upon what you're trying to return. But what might be confusing you is that if you issue a return
statement from inside the inDatabase
block, you're returning from the block, you're not returning from the method that contains this inDatabase
block.
所以,您只需不返回 inDatabase
块中的值,而是返回块外部的值。那么你通常会做的是,你将声明你的变量 inDatabase
块,你的 inDatabase
块会更新它,然后,当块完成时,就是当你返回结果时(而不是从 inDatabase
块中)。
So, you simply don't return values from the inDatabase
block, but rather you return values from outside the block. So what you'll commonly do, is you'll declare your variable to be returned outside the inDatabase
block, your inDatabase
block will update it, and then, when the block is done, that's when you return the results (not from within the inDatabase
block).
一个常见的例子是你正在构建一个 NSMutableArray
:所以在块之外创建可变数组,然后在块中添加值,但在退出 inDatabase
块之后返回结果:
A common example is if you're building an NSMutableArray
: So create the mutable array outside of the block, and then add values from within the block, but then return the results after you exit the inDatabase
block:
NSMutableArray *results = [NSMutableArray array]; // declare this outside the block
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath];
[queue inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", @(1)];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", @(2)];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", @(3)];
FMResultSet *rs = [db executeQuery:@"select * from foo"];
while ([rs next]) {
...
[results addObject:result]; // add values inside the block
}
[rs close];
}];
return results; // return the results outside the block
或者,如果你正在处理一些基本类型,比如一个 NSInteger
或 BOOL
或你有什么,你用 __块声明变量
限定符。例如,我将使用它来返回BOOL成功变量,例如:
Or, if you're dealing with some fundamental type, like a NSInteger
or BOOL
or what have you, you'd declare the variable with a __block
qualifier. For example, I'll use this to return a BOOL success variable, e.g.:
__block BOOL success; // again, define outside the block
NSMutableArray *results = [NSMutableArray array];
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath];
[queue inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", @(1)];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", @(2)];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", @(3)];
FMResultSet *rs = [db executeQuery:@"select * from foo"];
if (!rs)
{
NSLog(@"%s: %@", __FUNCTION__, [db lastErrorMessage]);
success = NO; // set the value inside the block
return; // note, this doesn't exit the method; this exits this `inDatabase` block
}
while ([rs next]) {
...
}
[rs close];
success = YES; // another example of setting that `success` variable
}];
// so whether I successfully completed the block, or whether I hit the `return`
// statement inside the block, I'll fall back here, at which point I'll return my
// boolean `success` variable
return success; // don't return the value until after you exit the block
虽然这可能看起来令人困惑第一你遇到它的时候,了解这一点很有用。当你开始大量使用GCD时,这种模式很常见。当你有一个块(由 ^
字符表示)时,你几乎不得不把它想象成你在里面定义的函数主要方法。当您在块中遇到 return
时,您将返回包含该块的方法。
While this might seem confusing the first time you encounter it, it's useful to understand this. When you start using GCD block a lot, this pattern is very common. When you have a block (indicated by the ^
character), you almost have to think of it as an function that you're defining inside your main method. When you encounter a return
inside a block, you're returning to the method that contains the block.
For块的介绍见:
这篇关于FMDatabaseQueue如何返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!