Project integration of the ring letter, click to contact customer service needs to adjust the interface, there is a request, if the login interface failed to call the ring, it is necessary to re-login, but this operation can not affect the main thread operation, the number of logins reached a certain amount after the stop login.
First of all: the Ring of the login synchronization, we need to open a thread, or when the ring letter login failure will be easy to cause the interface card dead situation.
+ (void) Loginwithsuccessblock: (void (^) ()) Success Failureblock: (void (^) ()) failure{//A public class in its own package
Turn on a thread to prevent a logon failure that causes the main thread to die
Nsoperationqueue *q = [[Nsoperationqueue alloc]init];
[Q addoperationwithblock:^{
Emerror *error = [[emclient sharedclient] loginwithusername:user_name password:@ "123456"];
Main thread Update UI
[[Nsoperationqueue Mainqueue] addoperationwithblock:^{
if (!error)
{
[[Emclient sharedclient].options Setisautologin:yes];
Success ();
}else{
Failure (); Login failed to pick another method to continue to log on, log on 5 times stop Login
[[Self alloc] init] hxloginfailed];
}
}];
}];
}
And then it's login failure. Open a thread to continue logging in, log on 5 times and stop logging
-(void) hxloginfailed
{
[Self.thread Cancel];
Self.thread = nil;
nsstring* countstr = [skuserdefaults valueforkey:@ "Hxcount"];
if ([Countstr intvalue]>5) {
Return
}
DLog (@ "========%@=====%d", [Nsthread currentthread],[countstr intvalue]);
Self.thread=[[nsthread alloc]initwithtarget:self selector: @selector (HXLOGINFAILED2) Object:nil];
[Self.thread start];
self.requestcount++;
}
-(void) HXLoginFailed2
{
Emerror *error = [[emclient sharedclient] loginwithusername:user_name password:@ "123456"];
if (Error) {
nsstring* countstr = [skuserdefaults valueforkey:@ "Hxcount"];
COUNTSTR = [NSString stringwithformat:@ "%d", [Countstr intvalue]+1];
[Skuserdefaults setvalue:countstr forkey:@ "Hxcount"];
[Self hxloginfailed];
} else {
}
}
------------------------------------------------------------------------Ornate Split Line-------------------------------------------- -------------------------------------------------
Finally, let's see how nsoperation.
Principle: As long as a nsoperation (the actual open in need to use its subclasses nsinvocationoperation, nsblockoperation) to nsoperationqueue this queue thread will be started in turn.
Nsoperation has two common subclasses for creating threading operations: Nsinvocationoperation and Nsblockoperation, which are essentially no different, but the latter using block form for code organization, using relatively convenient.
-(Instancetype) init
{
self = [super init];
if (self) {
Self. Operationqueue = [[Nsoperationqueue alloc]init];
Self. Operationqueue.maxconcurrentoperationcount = 1;
}
return self;
}
+ (void) Loginwithsuccessblock: (void (^) ()) Success Failureblock: (void (^) ()) failure{
Turn on a thread to prevent a logon failure that causes the main thread to die
Nsoperationqueue *q = [[Nsoperationqueue alloc]init];
[Q addoperationwithblock:^{
Emerror *error = [[emclient sharedclient] loginwithusername:user_name password:@ "jimaijie654321"];
Main thread Update UI
[[Nsoperationqueue Mainqueue] addoperationwithblock:^{
if (!error)
{
[[Emclient sharedclient].options Setisautologin:yes];
Success ();
}else{
Failure ();
[Skuserdefaults setvalue:@ "0" forkey:@ "Hxcount"];
[[Self alloc] init] hxloginfailed];
}
}];
}];
}
-(void) hxloginfailed
{
nsstring* countstr = [skuserdefaults valueforkey:@ "Hxcount"];
if ([Countstr intvalue]>5) {
Self. Operationqueue = nil;
Return
}
DLog (@ "========%@=====%d", [Nsthread currentthread],[countstr intvalue]);
[Self. Operationqueue addoperationwithblock:^{
DLog (@ "+++++++++++++%@=====%d", [Nsthread currentthread],[countstr intvalue]);
Emerror *error = [[emclient sharedclient] loginwithusername:user_name password:@ "jimaijie654321"];
Main thread Update UI
[[Nsoperationqueue Mainqueue] addoperationwithblock:^{
if (!error)
{
[[Emclient sharedclient].options Setisautologin:yes];
}else{
nsstring* countstr = [skuserdefaults valueforkey:@ "Hxcount"];
COUNTSTR = [NSString stringwithformat:@ "%d", [Countstr intvalue]+1];
[Skuserdefaults setvalue:countstr forkey:@ "Hxcount"];
[Self hxloginfailed];
}
}];
}];
}
Thread blocking Issues-Features: Automatic logon 5 times after a failed login