gpt4 book ai didi

ios - 如何在我的无处不在的容器中获取所有可用的持久存储?

转载 作者:行者123 更新时间:2023-11-29 03:28:08 25 4
gpt4 key购买 nike

在我的应用程序中,我有几个持久存储,用户可以删除和添加存储。我最近将 iCloud 添加到项目中,现在我需要知道哪些商店在启动时可用,以便我可以列出它们。以前,这是由我在创建和删除存储时管理的数组处理的。但现在可以创建或删除存储或单独的设备,我希望在我的无处不在的容器中获得所有可用的持久存储。这可能吗?

最佳答案

这是我用来查找新文件或已删除文件的代码。发布您的任何问题,我会尽力回复。

/*!  Creates and starts a metadata query for iCloud files

*/
- (void)createFileQuery {
[_query stopQuery];

if ([[CloudManager sharedManager] isCloudEnabled]) {
if (_query) {
[_query startQuery];
}
else {
_query = [[NSMetadataQuery alloc] init];

[_query setSearchScopes:[NSArray arrayWithObjects:NSMetadataQueryUbiquitousDocumentsScope, NSMetadataQueryUbiquitousDataScope, nil]];
NSString *str = @"*";
[_query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, str]];

NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(fileListReceived) name:NSMetadataQueryDidFinishGatheringNotification object:_query];
[notificationCenter addObserver:self selector:@selector(fileListReceived) name:NSMetadataQueryDidUpdateNotification object:_query];
[_query startQuery];
}
}

}

/*! Gets called by the metadata query any time files change

*/
- (void)fileListReceived {
//LOG(@"fileListReceived called.");

bool found = NO;

if ([self getNewiCloudDocs]) {
//LOG(@" New iCloud documents found!");
found = YES;
}

if ([self getDeletediCloudDocs]) {
//LOG(@" Deleted iCloud documents found!");
found = YES;
}

// Just check the local files again...
if (found)
[self updateList];

return;

}

/*! Gets the list of files in the iCloud directory and compares against files we have locally
and then creates new ones if required. WARNING: files appear in iCloud before they appear
locally when we are creating new documents so we must set a flag so we know to ignore any
new files that appear if we are busy creating a new document already. Only applies if we
have initiated creating a new document on this device!

@return Returns true if any have been found
*/
- (bool)getNewiCloudDocs {
//FLOG(@"getNewiCloudDocs called");
bool found = NO;

NSURL *iCloudDirectory = [[CloudManager sharedManager] iCloudCoreDataURL];

//FLOG(@" iCloudDirectory is %@", iCloudDirectory);

// Get the top level directory names are these will represent all the iCloud documents
NSArray* cloudDocuments = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:iCloudDirectory includingPropertiesForKeys:nil options:0 error:nil];

//Now get an array of the local document names including any UUID because we want then to be unique
NSMutableArray *localFileIDs = [[NSMutableArray alloc] init];

for (NSURL *doc in _localDocuments) {
[localFileIDs addObject:[doc lastPathComponent]];
}

// Check if there are any we don't already have
//FLOG(@" iCloud Documents are:");
for (NSURL* document in cloudDocuments) {
//FLOG(@" %@", [document lastPathComponent]);

NSString *name = [document lastPathComponent];

if (![localFileIDs containsObject:name]) {
//FLOG(@" new file found: %@", name);
found = YES;
[self createLocalCopyOfICloudFile: name];

}
}
return found;
}
/*! Looks for files we have that are not in iCloud and are local.
These have probably been deleted by some other device so remove them.

@return Returns true if any have been found
*/
- (bool)getDeletediCloudDocs {
//FLOG(@"getDeletediCloudDocs called");
bool found = NO;

NSURL *iCloudDirectory = [[CloudManager sharedManager] iCloudCoreDataURL];

//Now get an array of the local document names including any UUID because we want then to be unique
NSMutableArray *localFileIDs = [[NSMutableArray alloc] init];
NSMutableArray *cloudFileIDs = [[NSMutableArray alloc] init];


for (NSURL *doc in _localDocuments) {
[localFileIDs addObject:[doc lastPathComponent]];
}

// Get the top level directory names are these will represent all the iCloud documents
NSArray* cloudDocuments = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:iCloudDirectory includingPropertiesForKeys:nil options:0 error:nil];
if ([cloudDocuments count]) {

//FLOG(@" %d documents found in iCloud", [cloudDocuments count]);
//FLOG(@" docs are: %@", cloudDocuments);
for (NSURL* doc in cloudDocuments) {
[cloudFileIDs addObject:[doc lastPathComponent]];
}

// Check if there are documents that we have that are not in iCloud
//FLOG(@" Deleted iCloud Documents are:");
for (NSURL* document in _localDocuments) {
NSString *name = [document lastPathComponent];

if (![cloudFileIDs containsObject:name]) {
FLOG(@" REMOVED iCloud file detected: %@", name);
found = YES;
[self removeLocalCopyOfiCloudFile: name];

}
}
} else {

FLOG(@" no documents found in iCloud");
FLOG(@" removing all local documents");

for (NSURL* document in _localDocuments) {
NSString *name = [document lastPathComponent];
//FLOG(@" removing local file %@", name);
found = YES;
[self removeLocalCopyOfiCloudFile: name];
}

}

return found;
}

- (void)createLocalCopyOfICloudFile:(NSString *)fileName {

if (!_creatingDocument) {
NSURL* fileURL = [[[CloudManager sharedManager] documentsDirectoryURL] URLByAppendingPathComponent:fileName];

[self.appDelegate createLocalCopyOfCloudFile:fileURL];
} else {
LOG(@"Busy creating document so don't do anything!");
}
}

- (void)removeLocalFile:(NSString *)fileName {
LOG(@"");
FLOG(@" REMOVING local copy of file: %@", fileName);
NSURL* fileURL = [[[CloudManager sharedManager] documentsDirectoryURL] URLByAppendingPathComponent:fileName];
[self deleteDocumentAtURL:fileURL];
LOG(@"");
}

- (void)removeLocalCopyOfiCloudFile:(NSString *)fileName {
LOG(@"");
if (!_deletingDocument) {
FLOG(@" REMOVING local copy of DELETED iCloud file: %@", fileName);
NSURL* fileURL = [[[CloudManager sharedManager] documentsDirectoryURL] URLByAppendingPathComponent:fileName];
[self deleteLocalCopyOfiCloudDocumentAtURL:fileURL];
LOG(@"");
} else {
LOG(@"Busy deleting document so don't do anything!");
}
}

关于ios - 如何在我的无处不在的容器中获取所有可用的持久存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20190579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com