Platforms to show: All Mac Windows Linux Cross-Platform

/MacBase/NSFileHandle async reading


Required plugins for this example: MBS Util Plugin, MBS MacCocoa Plugin, MBS Main Plugin, MBS MacBase Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacBase/NSFileHandle async reading

This example is the version from Thu, 6th Apr 2016.

Project "NSFileHandle async reading.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() dim t as new timing("Creating file") dim data as string = RandomBytesStringMBS(filesize, true) dim path as string = "/tmp/NSFileHandle async reading.txt" dim f as FolderItem = GetFolderItem(path, FolderItem.PathTypeShell) dim b as BinaryStream = f.CreateBinaryFile("") b.Write data b.Close End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action() dim t as new timing("Reading file with binarystream") dim path as string = "/tmp/NSFileHandle async reading.txt" dim f as FolderItem = GetFolderItem(path, FolderItem.PathTypeShell) dim b as BinaryStream = f.OpenAsBinaryFile(false) dim s as string = b.Read(b.Length) if lenb(s)<>f.Length then MsgBox "Failed to read all data." end if End EventHandler
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action() dim t as new timing("Reading file with NSFileHandleMBS") dim path as string = "/tmp/NSFileHandle async reading.txt" dim f as FolderItem = GetFolderItem(path, FolderItem.PathTypeShell) dim n as NSFileHandleMBS = NSFileHandleMBS.fileHandleForReadingAtFile(f) dim s as string = n.readDataToEndOfFile if lenb(s)<>f.Length then MsgBox "Failed to read all data." end if End EventHandler
End Control
Control PushButton4 Inherits PushButton
ControlInstance PushButton4 Inherits PushButton
EventHandler Sub Action() gt = new timing("Reading file with NSFileHandleMBS in background") dim path as string = "/tmp/NSFileHandle async reading.txt" dim f as FolderItem = GetFolderItem(path, FolderItem.PathTypeShell) dim n as NSFileHandleMBS = NSFileHandleMBS.fileHandleForReadingAtFile(f) n.readToEndOfFileInBackgroundAndNotify End EventHandler
End Control
Control ProgressWheel1 Inherits ProgressWheel
ControlInstance ProgressWheel1 Inherits ProgressWheel
End Control
Control Slider1 Inherits Slider
ControlInstance Slider1 Inherits Slider
EventHandler Sub Open() FileSize=me.Value * 1024*1024 End EventHandler
EventHandler Sub ValueChanged() FileSize= me.Value *1024*1024 StaticText1.text=str(me.Value)+" MB" End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
EventHandler Sub Open() Center = NSNotificationCenterMBS.defaultCenter Observer=new MyNSNotificationObserverMBS Center.addObserver(Observer, NSFileHandleMBS.NSFileHandleReadToEndOfFileCompletionNotification) End EventHandler
Property FileSize As int64
Property Observer As MyNSNotificationObserverMBS
Property center As NSNotificationCenterMBS
Property gt As timing
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Class Timing
Sub Constructor(text as string) time=Microseconds me.text=text End Sub
Sub Destructor() dim t as Double = Microseconds-time MsgBox text+" needed "+Format(t/1000000.0, "0.000")+" seconds." End Sub
Property text As string
Property time As Double
End Class
Class MyNSNotificationObserverMBS Inherits NSNotificationObserverMBS
EventHandler Sub GotNotification(notification as NSNotificationMBS) if Notification.name=NSFileHandleMBS.NSFileHandleReadToEndOfFileCompletionNotification then dim userinfo as Dictionary = Notification.userInfo dim s as string = userinfo.Value(NSFileHandleMBS.NSFileHandleNotificationDataItem) window1.gt=nil MsgBox str(lenb(s))+" bytes read." end if End EventHandler
End Class
End Project

The items on this page are in the following plugins: MBS MacBase Plugin.


The biggest plugin in space...