Tip of the day: Count files in upload for iCloud

Some clients on macOS use Documents in the Cloud with their iCloud account. Files are synchronized with Apple’s server and that may take a while, so you can check how many files in a directory are uploading currently:

Shared Function CountUploadFiles(dir as FolderItem) As integer
	Dim count As Integer
	
	For Each file As FolderItem In dir.Children(False)
		
		Dim url As New NSURLMBS(file)
		
		If url.UbiquitousItemIsUploading Then
			count = count + 1
		End If
		
		If count > 100 Then
			Exit
		End If
	Next
	
	Return count
End Function

For two applications we now check that and if a lot of files are uploading, we slow down creating files, so the sync process can catch up.

If your application creates a log of files, it may be worth to consider marking temporary files as non synchronizable:

Shared Sub ExcludeFromSync(file as FolderItem)
	Dim url As New NSURLMBS(file)
	url.UbiquitousItemIsExcludedFromSync = True
End Sub

The example code uses new properties added for NSURLMBS class in MBS Xojo Plugins 21.1pr3.

2 Likes