繁体   English   中英

-[UIDragSession loadObjectsOfClass:completion:]完成块未调用

[英]-[UIDragSession loadObjectsOfClass:completion:] completionBlock not being called

我正在学习iOS11的新API拖放,但是我有一些疑问。 我有两个collectionViews,它们共享相同类型的dataSource数组( DataEntity )。 一个用于拖动,另一个用于拖放。 这意味着我想将包含数据( DataEntity )的项目从一个collectionView拖到另一个。

然后我在[-(id<UICollectionDropDelegate>)collectionView:performDropWithCoordinator:] 我无法获取在我的第一个collectionView中传递的数据( DataEntity ),因为未调用-[UIDragSession loadObjectsOfClass:completion:] 但是,如果我将其他类(例如UIImage.class或NSString.class)设置为参数loadObjectsOfClass:则会调用完成块,但是它不是兼容类,因此没有返回对象。

源代码

拖动collectionView

- (NSArray<UIDragItem *> *)collectionView:(UICollectionView *)collectionView itemsForBeginningDragSession:(id<UIDragSession>)session atIndexPath:(NSIndexPath *)indexPath {

    DataEntity* entity = self.entities[indexPath.row];
    UIDragItem* item = [[UIDragItem alloc] initWithItemProvider:[[NSItemProvider alloc] initWithObject:entity]];
    return @[item];
}

下降收集视图

- (void)collectionView:(UICollectionView *)collectionView performDropWithCoordinator:(id<UICollectionViewDropCoordinator>)coordinator {
    NSIndexPath* destinationIndexPath = coordinator.destinationIndexPath;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView performBatchUpdates:^{
            [coordinator.session loadObjectsOfClass:DataEntity.class completion:^(NSArray<__kindof id<NSItemProviderReading>> * _Nonnull objects) {
                [objects enumerateObjectsUsingBlock:^(__kindof id<NSItemProviderReading>  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [self.mutableEntities insertObject:(DataEntity*)obj atIndex:destinationIndexPath.row];
                    [self.collectionView insertItemsAtIndexPaths:@[destinationIndexPath]];
                }];
            }];
        } completion:nil];
    });
}
- (BOOL)collectionView:(UICollectionView *)collectionView canHandleDropSession:(id<UIDropSession>)session {
    BOOL test = [session canLoadObjectsOfClass:DataEntity.class]; // It's YES
    return test;
}

数据实体

- (NSProgress *)loadDataWithTypeIdentifier:(NSString *)typeIdentifier forItemProviderCompletionHandler:(void (^)(NSData * _Nullable, NSError * _Nullable))completionHandler {
    NSData* data = [NSKeyedArchiver archivedDataWithRootObject:self];
    completionHandler(data, nil);
    return nil;
}
+ (NSArray<NSString *> *)writableTypeIdentifiersForItemProvider {
    NSString* identifier = NSStringFromClass(self.class);
    return @[identifier];
}
+ (NSArray<NSString *> *)readableTypeIdentifiersForItemProvider {
    NSString* identifier = NSStringFromClass(self.class);
    return @[identifier];
}
+ (nullable instancetype)objectWithItemProviderData:(nonnull NSData *)data typeIdentifier:(nonnull NSString *)typeIdentifier error:(NSError * _Nullable __autoreleasing * _Nullable)outError {
    DataEntity* entity = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return entity;
}

编辑

好的,我发现了一些东西。 首先,如果删除dispatch_async(dispatch_get_main_queue()) ,我将得到一个错误的无法识别的选择器-[DataEntity encodeWithCoder:] 那是第二件事,我忘记使DataEntity符合NSCoding协议。

现在,新问题是,为什么我不能在dispatch_main_queue闭包中调用-[UIDragSession loadObjectsOfClass:completion:] ,否则将不会调用其完成块?

所有这些都与异步代码执行的基本原理有关。 在您的collectionView:performDropWithCoordinator:实现中,此代码是错误的:

dispatch_async(dispatch_get_main_queue(), ^{
    [coordinator.session loadObjectsOfClass: // ...

当您调用dispatch_async ,您允许对collectionView:performDropWithCoordinator:的调用返回—它结束了! 但是您要先进行此操作, 然后再继续加载数据。 因此, 您有机会提取数据之前 ,删除操作立即结束,会话消失。 当我们进入loadObjectsOfClass ,就不再有数据了。 会话已经结束。

实际上,我打赌那时的sessionnil 发送到nil对象的代码什么也没有做。 这就是为什么从不调用objectWithItemProviderData和完成处理程序的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM