Vahid
Hi,
I have an exception in my app, when i put the ls connection in background thread it app throw bad access exception that i can't handle it, also i can't put the calling code in workground because it takes too long to make connection with server,
Anyone has solution ?
gianluca.bertani
Hello,
could you be more specific? What exactly do you put in background that is causing a crash? Could you post here a code snippet? The iOS client library is designed to be thread-safe and can be used from any thread, we can surely find a solution for your problem.
Best regards,
Gianluca
Vahid
Dear Gianluca,
Thanks for your reply.
The code is as below:
//this line is for starting connection with lightstramer
- (void) viewDidAppear :Smile_Ac: BOOL)animated{
if (![self isConnected]) {
[self connectWithLightStreamer];
}
}
//check ls connection status
- (BOOL) isConnected
{
return client.connected;
}
//connect to ls in background
- (void)connectWithLightStreamer
{
[self performSelectorInBackground:@selector(connectInBackground) withObject:nil];
}
//connect to ls in background(private method)
- (void) connectInBackground
{
client = [[LSClient alloc] init];
LSConnectionInfo *connectioninfo = [LSConnectionInfo connectionInfoWithPushServerURL:LIGHTSTREAMER_CONNECITON_ADDRESS pushServerControlURL:nil user:@"user" password:@"password" adapter:LIGHTSTREAMER_CONNECITON_ADAPTER];
[client openConnectionWithInfo:connectioninfo delegate:self];
}
//subscribeitems
- (void) subscribeItems :Smile_Ac: NSArray *)itemnames andTableKey :Smile_Ac: LSSubscribedTableKey *)tableKey {
NSLog(@"StockListViewController: Subscribing table...");
@try {
//itemNames is equal to group
//fieldNames is equal to schema
LSExtendedTableInfo *tableInfo= [LSExtendedTableInfo extendedTableInfoWithItems:itemnames mode:LSModeMerge fields:fieldNames dataAdapter:LIGHTSTREAMER_CONNECITON_DATAADAPTER snapshot:YES];
tableInfo.requestedMaxFrequency= 1.0;
tableKey = [client subscribeTableWithExtendedInfo:tableInfo delegate:self useCommandLogic:NO];
[self.tableKeys addObject:tableKey];
[self.itemNames addObject:itemnames];
NSLog(@"StockListViewController: Table subscribed");
} @catch (NSException *e) {
NSLog(@"StockListViewController: Table subscription failed due to exception: %@", e);
}
}
//unsubscribe items
- (void) unsubscribeItems :Smile_Ac: LSSubscribedTableKey *)tableKey {
NSLog(@"StockListViewController: Unsubscribing table...");
@try {
[client unsubscribeTable:tableKey];
[self.itemNames removeAllObjects];
NSLog(@"StockListViewController: Table unsubscribed");
} @catch (NSException *e) {
NSLog(@"StockListViewController: Table unsubscription failed due to exception: %@", e);
}
}
//clear ls resource(call this method when viewWillDisappear or viewDidDisappear
- (void) clearLightStreamerResource
{
if (client.connected) {
for (LSSubscribedTableKey *key in self.tableKeys) {
[self unsubscribeItems:key];
}
[self.tableKeys removeAllObjects];
[client closeConnection];
}
}
Sometimes light streamer doesn’t respond when unsubscribing and causes the application stop working or freezing or crash, specifically by poor internet connectivity.
So, I cannot use light streamer in the application with more than one page using light streamer.
Kind Regards,
Vahid
gianluca.bertani
Hello Vahid,
I tested your code by putting it on a sample view controller, and it worked without problems. I can't attach files here on the forums, but feel free to contact support via e-mail and I will send it to you.
What you should pay attention to, is reconnections. The library contains a lot of logic to reconnect automatically and restart existing subscriptions, even in poor network conditions. So, let the library do it for you. If you really want to reconnect manually, the correct way to do this is described at the Automatic reconnections paragraph of the Getting started with the iOS Client library page (i.e.: the first page) of the library's documentation. Take a look there.
Hope this helps.
Best regards,
Gianluca