I am currently working on an application that is pretty similar to a chat app so what I did is I created my own server and in the app I am using streams to send messages to the server and retrieve messages from it.
I have many functions that require being triggered without me pressing a button or something else,for example when I try to get the online users that are my friends or groups I am enrolled in I want to get the result of this command when a view has loaded so I have the results before asking for them
I have tried this:
override func viewDidLoad() {
self.initNetworkCommunication()
self.button.sendActionsForControlEvents(UIControlEvents.TouchUpInside)
var indexSet = NSIndexSet(index: 0)
self.table.reloadSections(indexSet, withRowAnimation: UITableViewRowAnimation.Automatic)
self.table.reloadData()
super.viewDidLoad()
}
func initNetworkCommunication() {
var readStream:Unmanaged<CFReadStream>?;
var writeStream:Unmanaged<CFWriteStream>? ;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,"localhost",8023,&readStream,&writeStream)
inputStream = readStream!.takeUnretainedValue()
outputStream = writeStream!.takeUnretainedValue()
self.inputStream!.delegate=self
self.outputStream!.delegate = self
self.inputStream!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
self.outputStream!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
self.inputStream.open()
self.outputStream.open()
var msg = "iam:\(self.name).groupView\r\n" //i connect to the server in this view
var ptr = msg.nulTerminatedUTF8
var res = outputStream.write(msg, maxLength:msg.lengthOfBytesUsingEncoding(NSASCIIStringEncoding))
}
@IBAction func getGroups(sender : UIButton) {
println("it is pressed")
var msg = "mygroups:\(self.name):sdadsa"
var res = outputStream.write(msg, maxLength: msg.lengthOfBytesUsingEncoding(NSASCIIStringEncoding))
}
The issue I am having is that in some cases the touchUpInside event is triggered and I get the message I need from the server other times it doesn't
My first thought was that this happens because the server gets too many messages one after the other and gets "busy" but why does it work sometimes?
Any ideas or opinions will help me a lot