2016-05-04 13 views
-2

スウィフトの背景からフォアグラウンドに移動する場合、スウィフトの[nsObject copy]にはどのような方法が適していますか? スウィフトアレイをバックグラウンドからフォアグラウンドにコピー

は、Objective-Cの中に例えば、私たちは実行してバックグラウンドでALAssetsの長い配列(10,000のようなと言う)をループします:

[alGroup enumerateAssetsUsingBlock:^(ALAsset *alPhoto, NSUInteger index, BOOL *stop) 
    { 
    // Here to make changes for speed up image loading from device library... 

    // ===================================================== 
    // >>>>>>>>>>>>>>>>>>> IN BACKGROUND <<<<<<<<<<<<<<<<<<< 
    // ===================================================== 

    if(alPhoto == nil) 
    { 
     c(@"number of assets to display: %d", (int)bgAssetMedia.count); 
     // c(@"All device library photos uploaded into memory...%@", bgAssetMedia); 
     dispatch_async(dispatch_get_main_queue(), ^(void) 
     { 
     // ===================================================== 
     // >>>>>>>>>>>>>>>>>>> IN FOREGROUND <<<<<<<<<<<<<<<<<<< 
     // ===================================================== 
     [ui hideSpinner]; 

     if (_bReverse) 
     // Here we copying all the photos from device library into array (_assetPhotos)... 
     _assetPhotos = [[NSMutableArray alloc] initWithArray:[[[bgAssetMedia copy] reverseObjectEnumerator] allObjects]]; 
     else 
     _assetPhotos = [[NSMutableArray alloc] initWithArray:[bgAssetMedia copy]]; 

     // NSLog(@"%lu",(unsigned long)_assetPhotos.count); 
     if (_assetPhotos.count > 0) 
     { 
      result(_assetPhotos); 
     } 
     }); 
    } else { 
     // if we have a Custom album, lets remove all shared videos from the Camera Roll 
     if (![self isPhotoInCustomAlbum:alPhoto]) 
     { 
     // for some reason, shared glancy videos still show with 00:00 minutes and seconds, so remove them now 
     BOOL isVideo = [[alPhoto valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypeVideo]; 
     int duration = 0; 
     int minutes = 0; 
     int seconds = 0; 
     // NSString *bgVideoLabel = nil; 
     if (isVideo) 
     { 
      NSString *strduration = [alPhoto valueForProperty:ALAssetPropertyDuration]; 

      duration = [strduration intValue]; 
      minutes = duration/60; 
      seconds = duration % 60; 

      // bgVideoLabel = [NSString stringWithFormat:@"%d:%02d", minutes, seconds]; 
      if (minutes > 0 || seconds > 0) 
      { 
      [bgAssetMedia addObject:alPhoto]; 
      } 
     } else { 
      [bgAssetMedia addObject:alPhoto]; 
     } 
     } 
    } 
    // NSLog(@"%lu",(unsigned long)bgAssetMedia.count); 
    }]; 

をその後、我々は更新するフォアグラウンドに切り替えますこれらの行は、上記のスニペットであるのUIViewController、:

_assetPhotos = [[NSMutableArray alloc] initWithArray:[bgAssetMedia copy]]; 

「コピー」機能は、私たちはすぐに再び配列をループすることなく、バックグラウンドからフォアグラウンドにメモリをマーシャリングすることができ、黒魔術でした。 Swiftにも同様の方法がありますか?おそらく、このような何か:

_assetPhotos = NSMutableArray(array: bgAssetMedia.copy()) 

は背景から前景にメモリポインタを渡すために今スウィフトスレッド安全ですか?新しいプロトコルは何ですか?ありがとう -

答えて

0

私は答えを見つけました。 RealmおよびCoreDataデータベースコンテキストで大きなクエリを実行した後。私は簡単にメモリポインタの基本的なコピーを作成し、クラスに一致するようにそれをダウンキャストが見つかりました。文脈で

let mediaIdFG = mediaId.copy() as! String 

全例以下:

static func createOrUpdate(dictionary:NSDictionary) -> Promise<Media> { 
    // Query and update from any thread 
    return Promise { fulfill, reject in 
     executeInBackground { 
//  c("BG media \(dictionary)") 
     let realm:RLMRealm = RLMRealm.defaultRealm() 
     realm.beginWriteTransaction() 
     let media = Media.createOrUpdateInRealm(realm, withJSONDictionary:dictionary as [NSObject : AnyObject]) 
     // media.type = type 
     c("BG media \(media)") 
     let mediaId = media.localIdentifier 
     do { 
      try realm.commitWriteTransaction() 
      executeInForeground({ 
      let mediaIdFG = mediaId.copy() as! String 
      let newMedia = Media.findOneByLocalIdentifier(mediaIdFG) 
      c("FG \(mediaIdFG) newMedia \(newMedia)") 
      fulfill(newMedia) 
      }) 
     } catch { 
      reject(Constants.createError("Realm Something went wrong!")) 
     } 
     } 
    } // return promise 
    } // func createOrUpdate 

あなたは私の調査結果を知っているように私自身の答えを投稿。私はSwiftのコピー(別名objcのcopyWithZone:https://www.hackingwithswift.com/example-code/system/how-to-copy-objects-in-swift-using-copy

)に関するこの有益な記事も見つけました
関連する問題