How to create multi infinity loops with coroutine in Kotlin - loops

I have a method in viewmodel that I want to execute infinity till client stop that.
This loop should work for each button separately and stop that too.
But when I execute the loop for fourth time, application hangs.
How can I manage the loop and run it for four separate objects
This is my method in viewmodel:
fun getLocationInfinity(context: Context, tripId: Long, passengerId: Int) =
viewModelScope.launch {
val gpsTracker = LocationGpsTracker(context, 0, 0)
val netGpsTracker = LocationGpsTrackerNetwork(context)
var way = Way()
way.latitude1 = gpsTracker.getLatitude()
way.longitude1 = gpsTracker.getLongitude()
way.accuracy1 = gpsTracker.getAccuracy()
way.latitudeNet1 = netGpsTracker.getLatitude()
way.longitudeNet1 = netGpsTracker.getLongitude()
way.accuracyNet1 = netGpsTracker.getAccuracy()
while (isActive) {
if (_passengerSwitch.value?.get(passengerId - 1) == true) {
way.latitude2 = way.latitude1
way.longitude2 = way.longitude1
way.accuracy2 = way.accuracy1
way.latitudeNet2 = way.latitudeNet1
way.longitudeNet2 = way.longitudeNet1
way.accuracyNet2 = way.accuracyNet1
way.latitude1 = gpsTracker.getLatitude()
way.longitude1 = gpsTracker.getLongitude()
way.accuracy1 = gpsTracker.getAccuracy()
way.latitudeNet1 = netGpsTracker.getLatitude()
way.longitudeNet1 = netGpsTracker.getLongitude()
way.accuracyNet1 = netGpsTracker.getAccuracy()
_way.postValue(way)
val tripDetails = TripDetails(
latitude1 = way.latitude1,
latitude2 = way.latitude2,
longitude1 = way.longitude1,
longitude2 = way.longitude2,
accuracy1 = way.accuracy1,
accuracy2 = way.accuracy2,
latitudeNet1 = way.latitudeNet1,
latitudeNet2 = way.latitudeNet2,
longitudeNet1 = way.longitudeNet1,
longitudeNet2 = way.longitudeNet2,
accuracy1Net = way.accuracyNet1,
accuracy2Net = way.accuracyNet2,
distance = null,
isCalculated = false,
tripId = tripId.toInt(),
isEnded = false
)
localRepository.insertLocation(tripDetails)
delay(2000)
}
}
}

The delay() call needs to be outside your if-block. Otherwise, if the condition is false, this loop will never suspend so it never relinquishes the thread.

Related

2 object of same type returns true when != checked

I am using model.GetType().GetProperties() with foreach to compare properties of 2 object of same class.
like this
foreach (var item in kayit.GetType().GetProperties())
{
var g = item.GetValue(plu);
var b = item.GetValue(kayit);
if (g is string && b is string&& g!=b)
{
a += item.Name + "*";
}
else if (g is DateTime&& b is DateTime&& g!=b)
{
a += item.Name + "*";
}
}
But the problem is even if they have the same value g!=b returns a true all the time. I have used a break point to prove this and they are literally same thing. Actually I am taking the value putting it in textbox then creating another class after button click and comaring to see the changed properties. So even if I don't change anything it doesn't read the mas equals. Can someone help me about this please?
more info:
I get the plu from database and populate my control with it:
txtorder.Text = plu.OrderNo;
dtporder.Value = nulldate(plu.OrderDate);
dtp1fit.Value = nulldate(plu.FirstFitDate);
dtp1yorum.Value = nulldate(plu.FirstCritDate);
dtp2fit.Value = nulldate(plu.SecondFitDate);
dtp2yorum.Value = nulldate(plu.SecondCritDate);
dtpsizeset.Value = nulldate(plu.SizeSetDate);
dtpsizesetok.Value = nulldate(plu.SizeSetOkDate);
dtpkumasplan.Value = nulldate(plu.FabricOrderByPlan);
txtTedarikci.Text = plu.Fabric_Supplier;
dtpkumasFP.Value = nulldate(plu.FabricOrderByFD);
dtpfabarrive.Value = nulldate(plu.FabricArrive);
dtpbulk.Value = nulldate(plu.BulkFabricDate);
dtpbulkok.Value = nulldate(plu.BulkConfirmDate);
dtpaccessory.Value = nulldate(plu.AccessoriesDate);
dtpaccessoryarrive.Value = nulldate(plu.AccessoriesArriveDate);
dtpcutok.Value = nulldate(plu.ProductionStartConfirmation);
dtpcutstart.Value = nulldate(plu.ProductionStart);
dtpshipmentdate.Value = nulldate(plu.ShipmentDate);
dtpshipmentsample.Value = nulldate(plu.ShipmentSampleDate);
dtpshippedon.Value = nulldate(plu.Shippedon);
nulldate is just a method where I change null values to my default value.
And this is what I do after button click:
var kayit = new uretim();
kayit.OrderNo = txtorder.Text.ToUpper();
kayit.OrderDate = vdat(dtporder.Value);
kayit.FirstFitDate = vdat(dtp1fit.Value);
kayit.FirstCritDate = vdat(dtp1yorum.Value);
kayit.SecondFitDate = vdat(dtp2fit.Value);
kayit.SecondCritDate = vdat(dtp2yorum.Value);
kayit.SizeSetDate = vdat(dtpsizeset.Value);
kayit.SizeSetOkDate = vdat(dtpsizesetok.Value);
kayit.FabricOrderByPlan = vdat(dtpkumasplan.Value);
kayit.Fabric_Supplier = txtTedarikci.Text;
kayit.FabricOrderByFD = vdat(dtpkumasFP.Value);
kayit.FabricArrive = vdat(dtpfabarrive.Value);
kayit.BulkFabricDate = vdat(dtpbulk.Value);
kayit.BulkConfirmDate = vdat(dtpbulkok.Value);
kayit.AccessoriesDate = vdat(dtpaccessory.Value);
kayit.AccessoriesArriveDate = vdat(dtpaccessoryarrive.Value);
kayit.ProductionStartConfirmation = vdat(dtpcutok.Value);
kayit.ProductionStart = vdat(dtpcutstart.Value);
kayit.ShipmentDate = vdat(dtpshipmentdate.Value);
kayit.ShipmentSampleDate = vdat(dtpshipmentsample.Value);
kayit.Shippedon = vdat(dtpshippedon.Value);
kayit.Status = true;
kayit.WrittenDate = DateTime.Now;
kayit.GuidKey = plu.GuidKey != null ? plu.GuidKey : Guid.NewGuid().ToString("N");
I have proven by breakpoint that values are actually same. But the != check retruns a true.
When you are doing
g != b
compiler doesn't know that these objects are strings to compare so it compares their references. You can do:
g.Equals(b) //be carefull if one of them is null
or
g.ToString() != b.ToString()
EDIT
You can compare them after you check the type:
if (g is string && b is string)
{
if( g.ToString() != b.ToString() ){
}
}

construct an small app using shiny, after i code a loop it keeps giving me error

construct an small app using shiny, after i code a loop it keeps giving me error
Why am I getting this error"evaluation nested too deeply: infinite recursion / options(expressions=)?"
library(shiny)
ui<-fluidPage(
numericInput(inputId = "S0",label = "price at time 0",value = 20),
numericInput(inputId = "k",label = "exercise price",value = 20),
numericInput(inputId = "N",label = "steps",value = 50),
numericInput(inputId = "r",label = "riskfree rate",value = 0.03),
numericInput(inputId = "t",label = "time to exercise data",value = 1),
plotOutput(outputId = "v")
)
server<-function(input,output)
{
ve<-reactive({vector(mode="numeric",length=input$N+1)})
ve<-reactive({rep(input$N,9)})
for(i in 1:10)
{
ve<-reactive({c(ve(),i)})
}
output$v<-renderPlot({plot(ve())})
}
shinyApp(ui=ui,server=server)
it seems that something wrong with the for() sentence

lights out game using CORONA SDK

am trying to devalope a lights out game with CORONA SDK
but am not able to figure out a way for looping it !!!
how many functions to create and the way to keep this going
here is my code (its dummy but a friend gave it to me as am trying to go on from there )
obj = nil
px = 35
py = 50
r = 22
xi = 60
yi = 60
x1y1 = display.newCircle(px+xi*0,py+yi*0,r) x1y1.id = "x1y1"
x2y1 = display.newCircle(px+xi*1,py+yi*0,r) x2y1.id = "x2y1"
x3y1 = display.newCircle(px+xi*2,py+yi*0,r) x3y1.id = "x3y1"
x4y1 = display.newCircle(px+xi*3,py+yi*0,r) x4y1.id = "x4y1"
x5y1 = display.newCircle(px+xi*4,py+yi*0,r) x5y1.id = "x5y1"
x1y2 = display.newCircle(px+xi*0,py+yi*1,r) x1y2.id = "x1y2"
x2y2 = display.newCircle(px+xi*1,py+yi*1,r) x2y2.id = "x2y2"
x3y2 = display.newCircle(px+xi*2,py+yi*1,r) x3y2.id = "x3y2"
x4y2 = display.newCircle(px+xi*3,py+yi*1,r) x4y2.id = "x4y2"
x5y2 = display.newCircle(px+xi*4,py+yi*1,r) x5y2.id = "x5y2"
x1y3 = display.newCircle(px+xi*0,py+yi*2,r) x1y3.id = "x1y3"
x2y3 = display.newCircle(px+xi*1,py+yi*2,r) x2y3.id = "x2y3"
x3y3 = display.newCircle(px+xi*2,py+yi*2,r) x3y3.id = "x3y3"
x4y3 = display.newCircle(px+xi*3,py+yi*2,r) x4y3.id = "x4y3"
x5y3 = display.newCircle(px+xi*4,py+yi*2,r) x5y3.id = "x5y3"
x1y4 = display.newCircle(px+xi*0,py+yi*3,r) x1y4.id = "x1y4"
x2y4 = display.newCircle(px+xi*1,py+yi*3,r) x2y4.id = "x2y4"
x3y4 = display.newCircle(px+xi*2,py+yi*3,r) x3y4.id = "x3y4"
x4y4 = display.newCircle(px+xi*3,py+yi*3,r) x4y4.id = "x4y4"
x5y4 = display.newCircle(px+xi*4,py+yi*3,r) x5y4.id = "x5y4"
x1y5 = display.newCircle(px+xi*0,py+yi*4,r) x1y5.id = "x1y5"
x2y5 = display.newCircle(px+xi*1,py+yi*4,r) x2y5.id = "x2y5"
x3y5 = display.newCircle(px+xi*2,py+yi*4,r) x3y5.id = "x3y5"
x4y5 = display.newCircle(px+xi*3,py+yi*4,r) x4y5.id = "x4y5"
x5y5 = display.newCircle(px+xi*4,py+yi*4,r) x5y5.id = "x5y5"
bb = {x1y1,x2y1,x3y1,x4y1,x5y1,x1y2,x2y2,x3y2,x4y2,x5y2,x1y3,x2y3,x3y3,x4y3,x5y3,x1y4,x2y4,x3y4,x4y4,x5y4,x1y5,x2y5,x3y5,x4y5,x5y5}
iClicked = 0
function click(e)
if(e.phase == "ended") then
--circleID = e.target.id
--whichCircle()
print(e.target.id)
obj = e.target
for u=1,25 do
if(obj==bb[u]) then
iClicked = u
end
end
if((iClicked-5) > 0 and (iClicked-5) < 26) then
bb[iClicked-5]:setFillColor(1,0,0)
end
if((iClicked-1) > 0 and (iClicked-1) < 26) then
bb[iClicked-1]:setFillColor(1,0,0)
end
obj:setFillColor(1,0,0)
if((iClicked+1) > 0 and (iClicked+1) < 26) then
bb[iClicked+1]:setFillColor(1,0,0)
end
if((iClicked+5) > 0 and (iClicked+5) < 26) then
bb[iClicked+5]:setFillColor(1,0,0)
end
end
end
for k=1,25 do
bb[k]:addEventListener("touch",click)
end
its all about having 25 circles and lighting them on and off but it doesnt seem to work for me
any good help will be great
Thanks
local myCircles = {}
for y = 1, 5 do
myCircles[y] = {}
for x = 1, 5 do
myCircles[y][x] = display.newCircle(px+xi*0,py+yi*4,r)
myCircles[y][x].id = .id = "x" .. x .. "y" .. y
end
end
or something like that.
Rob

Yii - CDbCriteria unexpected results

I am doing what looks like a simple query basically doing a WHERE clause on the competition_id and the prize_type
$criteria = new CDbCriteria;
$criteria->select = 't.*, myuser.firstname, myuser.surname';
$criteria->join ='LEFT JOIN myuser ON myuser.user_id = t.user_id';
$criteria->condition = 't.competition_id = :competition_id';
$criteria->condition = 't.prize_type = :prize_type';
$criteria->params = array(":competition_id" => $competition_id);
$criteria->params = array(":prize_type" => "1");
$winners = CompetitionWinners::model()->findAll($criteria);
Can anyone suggest what is wrong with my code... I am expecting around 4 rows.. but get over 600?
I just want to do ...
WHERE competition_id = 123 AND prize_type = 1;
Is there a simple function to simply output the SQL query for this SINGLE CDbCriteria 'event'?
try this
$criteria = new CDbCriteria;
$criteria->select = 't.*, myuser.firstname, myuser.surname';
$criteria->join ='LEFT JOIN myuser ON myuser.user_id = t.user_id';
$criteria->condition = 't.competition_id = :competition_id AND t.prize_type = :prize_type';
$criteria->params = array(":competition_id" => $competition_id,":prize_type" => "1");
$winners = CompetitionWinners::model()->findAll($criteria);
Or you could use CDbCriteria::addCondition()
$criteria->addCondition('t.competition_id = :competition_id')
->addCondition('t.prize_type = :prize_type');

Wow addon failing to work with array

I'm trying to create a simple addon for world of warcraft which records my kills.
I've already got'n quite far except there is a problem with the writing of a lua array.
The code I have so far
local CharacterDefaults = {
kills = {},
totalkills = 0
}
local killDefaults = {
DBtimeofday = 0,
DBplayer = 0,
DBenemyname = 0,
DBenemyid = 0,
DBzone = 0,
DBkilltype = 0
}
The next piece is inside a event which checks for overkill
if not KillCount then
KillCount = CharacterDefaults
end
if not KillCount.totalkills then
KillCount.totalkills = 0
end
KillCount.enemy[KillCount.totalkills] = destName
KillCount.kills[KillCount.totalkills] = killDefaults
KillCount.kills[KillCount.totalkills].DBtimeofday = stamp
KillCount.kills[KillCount.totalkills].DBzone = zone
KillCount.kills[KillCount.totalkills].DBkilltype = killtype
KillCount.kills[KillCount.totalkills].DBenemyid = unitId
KillCount.kills[KillCount.totalkills].DBenemyname = destName
KillCount.kills[KillCount.totalkills].DBplayer = playerName
KillCount.totalkills = KillCount.totalkills + 1
Ofcourse there's more code but this is the only important code (as far as I know).
If I look at this I would expect that for every new kill a new array part is made and the values are entered. However, for each kill I make in world of warcraft, every single item already in it will get the results of the last kill.
The lua variables saved file:
KillCount = {
["kills"] = {
{
["DBplayer"] = "MyName",
["DBzone"] = "Blackrock Depths",
["DBkilltype"] = 0,
["DBenemyname"] = "Grim Patron",
["DBenemyid"] = 9545,
["DBtimeofday"] = "11-09-22 10:45:23",
}, -- [1]
{
["DBplayer"] = "MyName",
["DBzone"] = "Blackrock Depths",
["DBkilltype"] = 0,
["DBenemyname"] = "Grim Patron",
["DBenemyid"] = 9545,
["DBtimeofday"] = "11-09-22 10:45:23",
}, -- [2]
[0] = {
["DBplayer"] = "MyName",
["DBzone"] = "Blackrock Depths",
["DBkilltype"] = 0,
["DBenemyname"] = "Grim Patron",
["DBenemyid"] = 9545,
["DBtimeofday"] = "11-09-22 10:45:23",
},
},
["totalkills"] = 3,
}
as you can see the [0] is the only one to be properly writen. Am I doing something wrong?
The problem is here:
KillCount.kills[KillCount.totalkills] = killDefaults
Everytime you kill, you're pointing KillCount.kills[KillCount.totalkills] to killDefaults then modifying killDefaults. The problem is, you are using the same killDefaults every time. So when you udpate the values of killDefaults later, it affects every reference to killDefaults that you have already created.
Try something like:
function GetDefaultKills()
return {
DBtimeofday = 0,
DBplayer = 0,
DBenemyname = 0,
DBenemyid = 0,
DBzone = 0,
DBkilltype = 0
};
end
KillCount.kills[KillCount.totalkills] = GetDefaultKills()

Resources