0

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

tudoricc
  • 709
  • 1
  • 12
  • 31
  • Your first thought could be right only if the "it pressed" doesn't get logged. Also, you could be stuck in the "UI is blocked" (the main Thread that manage the UI is processing somewhere and can't get the event). Is your button "big enough"? That could be also an issue if it's too small for finger (recommendation from Apple were to use at least 40*40 px if I remember well). – Larme Sep 18 '14 at 09:17
  • I found a way to kinda fix this.I don't know if it's 100% user friendly or safe so ...Ok.the ideea is that I send to the server every x seconds a message that should be the same thing I would have sent to the server by pressing the button. Is it ok or wrong? – tudoricc Sep 18 '14 at 09:45

0 Answers0