@@ -472,28 +472,44 @@ - (nullable ETCoreMLAsset *)compiledModelAssetWithMetadata:(const ModelMetadata&
472472    __block ETCoreMLAsset *compiledModelAsset = [self  assetWithIdentifier: identifier];
473473    if  (compiledModelAsset) {
474474        ETCoreMLLogInfo (" Cache Hit: Successfully retrieved compiled model with identifier=%@ from the models cache."  , identifier);
475-     } else  {
476-         ETCoreMLLogInfo (" Cache Miss: Compiled Model with identifier=%@ was not found in the models cache."  , identifier);
475+         return  compiledModelAsset;
477476    }
478- 
477+     
478+     ETCoreMLLogInfo (" Cache Miss: Compiled Model with identifier=%@ was not found in the models cache."  , identifier);
479+     __block NSURL  *compiledModelURL;
479480    [self .assetManager withTemporaryDirectory: ^(NSURL  * _Nonnull directoryURL) {
480-         if  (compiledModelAsset) {
481-             return ;
482-         }
483- 
484481        //  The directory specified by `directoryURL` is unique and will be automatically cleaned up
485482        //  once the enclosing block completes.
486-         NSURL  * compiledModelURL = [self  compiledModelURLWithIdentifier: identifier
483+         compiledModelURL = [self  compiledModelURLWithIdentifier: identifier
487484                                                              modelURL: modelURL
488485                                                            inMemoryFS: inMemoryFS
489486                                                                dstURL: directoryURL
490487                                                                 error: error];
491488        if  (compiledModelURL) {
492489            //  Move the compiled model to the asset manager to transfer ownership.
490+             ETCoreMLLogInfo (" Successfully compiled model with identifier=%@.  Transferring ownership to assetManager."  , identifier);
493491            compiledModelAsset = [self .assetManager storeAssetAtURL: compiledModelURL withIdentifier: identifier error: error];
494492        }
495493    }];
496494
495+     if  (!compiledModelAsset) {
496+         ETCoreMLLogInfo (" Failed to transfer ownership of asset with identifier=%@ to assetManager"  , identifier);
497+         if  (compiledModelURL && [self .fileManager fileExistsAtPath: compiledModelURL.path]) {
498+             //  Log what error was since we now attempt backup path, and previous error is overwritten
499+             if  (error && *error) {
500+                 ETCoreMLLogInfo (" error=%@"  , (*error).localizedDescription );
501+                 *error = nil ;
502+             }
503+             ETCoreMLLogInfo (" Attempting to fall back by loading model without transferring ownership"  );
504+             auto  backingAsset = Asset::make (compiledModelURL, identifier, self.assetManager .fileManager , error);
505+             if  (backingAsset) {
506+                 compiledModelAsset = [[ETCoreMLAsset alloc ] initWithBackingAsset: backingAsset.value ()];
507+             }
508+         }
509+     }
510+ 
511+     //  compiledModelAsset can still be nil if our backup path failed
512+ 
497513    return  compiledModelAsset;
498514}
499515
@@ -582,10 +598,9 @@ - (nullable ETCoreMLAsset *)modelAssetWithMetadata:(const ModelMetadata&)metadat
582598        return  nil ;
583599    }
584600
585-     ETCoreMLModel *model = [ETCoreMLModelLoader loadModelWithContentsOfURL : compiledModelAsset.contentURL 
601+     ETCoreMLModel *model = [ETCoreMLModelLoader loadModelWithCompiledAsset : compiledModelAsset
586602                                                             configuration: configuration
587603                                                                  metadata: metadata
588-                                                               assetManager: self .assetManager
589604                                                                     error: error];
590605    if  (!model) {
591606        return  nil ;
0 commit comments