对该教程的简要介绍表明它正在谈论神奇的记录。我从来没有用过,所以我不能说它。
// This declares a class method that returns void and takes a block as parameter.
// The block returns void and has one parameter, namely, a pointer to an
// NSManagedObjectContext object.
+ (void)saveDataInContext:(void(^)(NSManagedObjectContext *context))saveBlock;
你会这样调用那个方法......
[SomeClass saveDataInContext:^(NSManagedObjectContext *context){
// Some code
}];
这意味着您正在将一段代码传递给该函数。在某些时候,它会执行你给它的代码块。当它这样做时,它会将托管对象上下文传递到块中,以便它可以用它做一些事情。
现在,看看该方法的实现......
+ (void)saveDataInContext:(void(^)(NSManagedObjectContext *context))saveBlock
{
// Create a MOC - note there is no concurrency type, so it will get
// NSConfinementConcurrencyType, which means it must be used exclusively
// from the thread in which it was created. Since it is a local variable
// and it gets destroyed after this function is called, that should be cool
// PROVIDED the using block does not do anything untoward with it.
NSManagedObjectContext *context = [NSManagedObjectContext context];
// Set the merge policy
[context setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
// MR must set some default context...
// Some how the above context needs a persistent store to save...
[defaultContext setMergePolicy:NSMergeObjectByPropertyStoreTrumpMergePolicy];
// Probably setting up notification handler for DidSave
[defaultContext observeContext:context];
// Now, this is where the block you passed in gets called.
// Note, that the managed object context has already been setup for you.
// Now that it's setup, the block of code that you passed in is going
// to be called, and it will be given a context that it can use to execute
// code in the calling thread.
block(context);
// If you changed something to the context in your block of code, the save.
if ([context hasChanges])
{
[context save];
}
}
让我们回顾一下调用这个方法的代码......
[SomeClass saveDataInContext:^(NSManagedObjectContext *context){
// Now, the saveDataInContext method has been called. However, inside
// that method, a call was made to the block that was passed in.
// That would be this here block of code. So, if you look up in
// the method, where is calls "block(context)" this block of code will
// be executed right there. Mentally, you can cut and paste this code
// in that spot.
// The context parameter is the context that was passed to this block.
// you can use it to do any Core Data stuff...
}];
现在,这段代码非常相似,但它需要两个块。一个用于在上下文中执行一些代码,另一个是异步保存完成后将执行的块。
saveBlock 应该很熟悉。这与上面示例中的概念相同。
完成是一个块,它返回 void,并且不接受参数。完成所有工作后,它将被调用。
+ (void)saveDataInBackgroundWithContext:(void(^)(NSManagedObjectContext *context))saveBlock completion:(void(^)(void))completion
{
// Dispatch some work on one of the global concurrent queues. It will
// get done on some thread, nobody knows which one, but it does not matter
// because the code in this block calls saveDataInContext, and passes the
// block it was given that does some modifications to the context.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self saveDataInContext:saveBlock];
// Now, when the above work is done, we are still running in some random
// thread. I guess the library wants all callbacks to happen on the main
// thread, so this block is dispatched on the main thread. Note that it
// calls the second bock passed in as the completion block.
// So the <saveBlock> will be run on some random thread, and then, when
// it is done, the <completion> block will be called on the main thread.
dispatch_sync(dispatch_get_main_queue(), ^{
completion();
});
});
}
像之前一样,当您调用该方法时,您可以在脑海中将 替换为您传入的第一个块,然后替换为第二个块。
[NSManagedObjectHelper saveDataInBackgroundWithContext:^(NSManagedObjectContext *localContext){
// This is the first block. It gets executed where you see <saveBlock>
// being used in the earlier method. You are being given the already
// prepared MOC, and it's name is <localContext>. Do your managed object
// context stuff with it. Note that it will be running in some unknown thread.
for (NSDictionary *personInfo in listOfPeople)
{
PersonEntity *person = [PersonEntity createInContext:localContext];
[person setValuesForKeysWithDictionary:personInfo];
}
} completion:^{
// Now, this is the second block, which is run when all the core data saving
// has been completed. It will run on the main thread.
self.people = [PersonEntity findAll];
}];
希望这可以帮助您了解正在发生的事情,即使我不知道神奇唱片在幕后的真正作用。
编辑
回应这个评论...
我想我不明白这些块是如何工作的。如果一个块具有此方法签名“+ (void)saveDataInContext:(void(^)(NSManagedObjectContext *context))saveBlock”,为什么该块不使用“context”或“saveBlock”?哪个是块的返回值,哪个是传入的值?– 迈克小号
首先,该块没有这个签名......
+ (void)saveDataInContext:(void(^)(NSManagedObjectContext *context))saveBlock;
那是一个类方法。让我们一块一块地分解。然而,首先,让我们忘记块参数,并使用一些简单的东西来进行比较。
+ (void)foo:(NSInteger)someInteger;
即 a 是一个类方法,foo:
它返回void
并接受一个参数。该单个参数的类型是NSInteger
. 如果我想调用它,我会这样做:
[SomeClass foo:42];
同样地...
+ (void)saveDataInContext:(void(^)(NSManagedObjectContext *context))saveBlock;
是一个类方法,saveDataInContext:
它返回 void 并接受一个参数。该单个参数的类型是void(^)(NSManagedObjectContext *context)
.
现在,不要让那个狼吞虎咽的人愚弄你。它只是一种类型(尽管如果你不太了解 C 的话,解析起来会有些混乱)那么,什么是void(^)(NSManagedObjectContext *context)
首先,它是一个block
. 如果那(^)
之后void
是(*)
一个函数指针。
基本上,这意味着该参数的类型是 a block
,它返回void
并具有一个参数,即指向 a 的指针NSManagedObjectContext
(带有 name context
)。
所以,如果我们大声读出来...
+ (void)saveDataInContext:(void(^)(NSManagedObjectContext *context))saveBlock;
是一种类方法,选择器saveDataInContext:
返回void
并具有一个参数,该参数具有名称saveBlock
并且属于“返回 void 并具有一个类型参数的块NSManagedObjectContext *
”。
就像我们这样称呼第一个例子......
[SomeClass foo:42];
我们这样称呼后一个例子......
[SomeClass saveDataInContext:^(NSManagedObjectContext *context){
// We are creating a bock of code, so stuff some code in here.
}];
现在,就像您将整数传递42
给foo:
您一样,您将块之间的块{}
作为参数传递给saveDataInContext:
.
现在,请注意该saveDataInContext:
方法的签名需要一个本身具有参数的块。所以,当你提供你的块时,你基本上是在说,“嘿,这里有一段代码供你调用,当你这样做时,请确保给我一个指向 NSManagedObjectContext
我可以使用的对象的指针。
这意味着当你的块被调用时,调用代码将调用你的块并为NSManagedObjectContext *
你提供一个变量 name context
。
可以这样想,作为saveDataInContext:
.
+ (void)saveDataInContext:(void(^)(NSManagedObjectContext *context))saveBlock {
// Create a context to give the block we are going to call..
NSManagedObjectContext *moc = //
saveBlock(moc);
}
现在,当您的代码被调用时,您将获得该moc
对象作为您的参数。基本上,该方法创建一个托管对象上下文,执行所有线程安全工作,然后调用您的代码块,并为您提供指向它已安全创建的托管对象上下文的指针。您的代码在该安全环境的范围内执行,使用作为函数(块)参数传递给它的 MOC。
我希望这没有让事情变得更糟......